[英]Shared preference always taking default value
I always get default in my shared preference , why is this happening? 我的共享偏好总是违约,为什么会这样? Here is the part where I insert value:
这是我插入值的部分:
holder.camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RowData rd = getItem(position); //get list_row from i
System.out.println("OnClick Camera");
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
SharedPreferences prefs = (mContext).getSharedPreferences(
"com.oxtro.trustea", Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("crit_id_pref",String.valueOf(rd.criteria_id));
prefEditor.commit();
((Activity)mContext).startActivityForResult(takePicture, 0);
}
});
Here is where I retrieve its value, the value is always fetched as default rather than the needed one: 这是我检索其值的位置,该值始终作为默认值而不是所需的值获取:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
Uri uri = data.getData();
System.out.println("File path is " + uri.toString());
String path = getRealPathFromURI(uri);
System.out.println("Real path is " + path);
imageupload= new ImageUploadManager(ChapterActivity.this);
imageupload.open();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ChapterActivity.this);
String t_critid = preferences.getString("crit_id_pref", "DEFAULT");
System.out.println("@OnActivityResult | shared pref crit id: "+t_critid);
}
Make this call in your second activity too, to get the result: 也可以在第二个活动中进行此调用,以获得结果:
SharedPreferences prefs = (mContext).getSharedPreferences(
"com.oxtro.trustea", Context.MODE_PRIVATE);
In onActivityResult
, 在
onActivityResult
,
Change this: 改变这个:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ChapterActivity.this);
To this: 对此:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences("com.oxtro.trustea", Context.MODE_PRIVATE);
You need to keep the Preference your referencing consistent. 您需要保持Preference您的引用一致。 When you write to a Preference with one name, you need to read from it with the same name as well.
当您使用一个名称写入Preference时,您需要使用相同的名称从中读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.