共享偏好问题

时间:2011-03-09 17:47:49

标签: android sharedpreferences

我在将变量放入共享首选项时遇到问题。我从联系人选择器中获取联系人ID,姓名和号码,然后将其保存到共享首选项,以便我可以在另一个活动中使用它们放入数据库但是每次我去获取其他活动时它会给我当我要求姓名或身份证时的电话号码?

即。联系信息活动

id "displays the id"
name "displays name"
phone "displays phone"

第二项活动

id "displays phone number"
name "displays phone number"
phone "displays phone number"

我不明白吗?

这是我的代码

偏好等级

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);
}

}

获取联系信息摘要

    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();
}

如果我仍然在此活动中从共享首选项中获取3个值,则会获得正确的值

第二个活动代码段

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);
        }
    });

我不明白它是如何变化的,当我使用它们时只有2次

2 个答案:

答案 0 :(得分:1)

我猜你的SmsPrefs.ID.NAME.NUMBER是相同的值。它们需要不同,否则会相互覆盖。

你可以:

  • 更改您编写数据的顺序,在姓名和ID之前写下电话号码。这应该会改变您的结果并确认问题。
  • 在调试器中逐步浏览应用程序以查看这些常量的实际值
  • 发布您的SmsPrefs代码供我们调查。

尝试将SmsPrefs常量更改为:

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";

您的密钥实际上必须彼此不同。

答案 1 :(得分:1)

public static final String ID = "";
public static final String NAME = "";
public static final String NUMBER = "";

那些需要设置为:

public static final String ID = "_id";
public static final String NAME = "_name";
public static final String NUMBER = "_number";