I am having a problem putting a variable to shared preferences. I am getting a contacts id,name and number from the contact picker then saving them to a shared preference so that i can use them in another activity to 开发者_如何学Pythonput in a database but every time i go to get them in that other activity it gives me the phone number when i ask for the name or id??
ie. contact info activity
id "displays the id"
name "displays name"
phone "displays phone"
second activity
id "displays phone number"
name "displays phone number"
phone "displays phone number"
i dont get it?
here is my code
preference class
public class SmsPrefs extends PreferenceActivity {
public static final String ID = "";
public static final String NAME = "";
public static final String NUMBER = "";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.sms_pref);
}
}
getting contact info snippet
public void getContactData(Intent data){
Context context = this;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = pref.edit();
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Log.v("Contact", contactData.toString());
Cursor c = managedQuery(contactData,null,null,null,null);
if(c.moveToFirst()){
id = contactData.getLastPathSegment();
editor.putString(SmsPrefs.ID, id);
editor.commit();
Log.v("Contact", "ID: " + id.toString());
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
editor.putString(SmsPrefs.NAME, name);
editor.commit();
Log.v("Contact", "Name: " + name.toString());
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);
if(pCur.moveToFirst()){
phone = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
editor.putString(SmsPrefs.NUMBER, phone);
editor.commit();
Log.v("Contact", "Phone Number: " + phone.toString());
}
pCur.close();
}
}
c.close();
}
if i get the 3 values from shared preferences while still in this activity i get the correct values
second activity snippet
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = new String ("name");
String id = new String ("id");
String phone = new String ("number");
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(v.getContext());
id = pref.getString(SmsPrefs.ID, "");
name = pref.getString(SmsPrefs.NAME, "");
phone = pref.getString(SmsPrefs.NUMBER, "");
Log.v("EditContact", "ID: " + id);
Log.v("EditContact", "Name: " + name);
Log.v("EditContact", "Phone: " + phone);
db.open();
db.deleteContact(7);
db.close();
db.open();
long _id;
_id = db.insertContact(phone, name, nColor, nVibrate, ringTonePath);
db.close();
Log.v("EditContact", "ID: " + _id);
Intent ok = new Intent(EditContact.this,Contacts.class);
startActivity(ok);
}
});
i dont understand how it is changing when those are the only 2 times i use them
I'm guessing your SmsPrefs.ID
, .NAME
and .NUMBER
are the same value. They need to be different or they will overwrite each other.
You can:
- Change the order in which you write the data, writing the phone number before the name and id. This should alter your results and confirm the problem.
- Step through the app in the debugger to see what the actual values of those constants are
- Post your
SmsPrefs
code for us to investigate.
Try changing your SmsPrefs
constants to:
public static final String ID = "com.example.SmsPrefs.ID";
public static final String NAME = "com.example.SmsPrefs.NAME";
public static final String NUMBER = "com.example.SmsPrefs.NUMBER";
Your keys actually have to be different from one another.
public static final String ID = "";
public static final String NAME = "";
public static final String NUMBER = "";
Those need to be set to something like:
public static final String ID = "_id";
public static final String NAME = "_name";
public static final String NUMBER = "_number";
精彩评论