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 put 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,.NAMEand.NUMBERare the same value. They need to be different or they will overwrite each other.You can:
SmsPrefscode for us to investigate.Try changing your
SmsPrefsconstants to:Your keys actually have to be different from one another.