Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4032638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:44:47+00:00 2026-05-20T11:44:47+00:00

I am having a problem putting a variable to shared preferences. I am getting

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:44:47+00:00Added an answer on May 20, 2026 at 11:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble putting this problem into searchable terms. I'm working on an Android
I just solved a problem I was having putting the Set keyword in a
I'm having a problem in calling a function that returns a result, from another
I am having a problem putting a java applet on a website using Dreamweaver
I'm having a problem on putting onclick event on a replaceWith on jquery. My
the problem I'm having is to validate the input means putting it in a
im having a slight problem putting if statements inside if statments e.g: <?php $steponeimage
I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into
I am having a problem with JNI, calling a method from C++ to Java.
I'm having a problem getting a controller method that returns a JsonResult to accept

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.