共享preferences? (非常简单的问题!?) [英] Shared preferences? (Extremely simple issue!?)
问题描述
我只是想保存在共享preference从EDITTEXT用户的输入,但它不工作:
editText.setOnEditorActionListener(新TextView.OnEditorActionListener(){
@覆盖
公共布尔onEditorAction(TextView的V,INT键code,KeyEvent的事件){ Log.v(TAG,keyword.getString(关键字,mDefault)); //它甚至注销默认字符串** **后存储preFERENCES前 如果(键code == EditorInfo.IME_ACTION_SEND){
editText.setText(editText.getText()的toString()); keywordEditor.putString(关键词,editText.getText()的toString());
keywordEditor.commit(); Log.v(TAG,keyword.getString(关键字,默认)); //正确!此线路工作
}
}
返回true;
});
当我第一次编辑的文字,我会先得到一个日志的 mDefault
,这是正常的,因为没有什么是存储在共享preference。
然后,我存储在共享preference东西,以确保它储存,我登录,我得到的日志我所输入的。 这意味着共享preference数据储存。
继承人的问题:我已经存储在共享preference东西后,我去一个不同的活动,我回来了,存储在共享$ P $的所有数据pference为不见了!
的第一个日志还称 mDefault
后通过各种活动进行导航。
还有什么问题呢?
编辑:
下面是我的实例:
的onCreate
:
关键字= preferenceManager.getDefaultShared preferences(本); //创建共享preferences
keywordEditor = keyword.edit();
添加本作中,如果你错过了一个关键组件的例子。这是目前为我工作:
公共类Main2Activity扩展ActionBarActivity { 私人共享preferences关键字;
私人共享preferences.Editor keywordEditor;
私人字符串标记=TAG; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main2);
关键字= preferenceManager.getDefaultShared preferences(本); //创建共享preferences
keywordEditor = keyword.edit(); 最终的EditText EDITTEXT =(EditText上)findViewById(R.id.et_text);
findViewById(R.id.btn_launch).setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新意图(Main2Activity.this,Main22Activity.class);
startActivity(意向);
}
}); editText.setOnEditorActionListener(新TextView.OnEditorActionListener(){
@覆盖
公共布尔onEditorAction(TextView的V,INT键code,KeyEvent的事件){
Log.v(TAG,初始+ keyword.getString(关键词,mDefault)); //它甚至注销默认字符串** **后存储preFERENCES前 如果(键code == EditorInfo.IME_ACTION_SEND){
editText.setText(editText.getText()的toString()); keywordEditor.putString(关键词,editText.getText()的toString());
keywordEditor.commit(); Log.v(TAG,在preFS保存:+ keyword.getString(关键词,默认)); //正确!此线路工作
}
返回true;
}
});
}}
这是一个全新的安装:
我测试类型的,点击发送按钮键盘上,因此调用 onEditorAction
然后在单击推出新的活动 - >打回按钮,然后在TEST2类型的,点击发送按钮。
以下是注销认沽:
02-29 23:26:42.068 18105-18105 / com.example.naveed.myapplication V / TAG:初始:mDefault
02-29 23:26:42.136 18105-18105 / com.example.naveed.myapplication V / TAG:保存在preFS:测试02-29 23:26:53.281 18105-18105 / com.example.naveed.myapplication V / TAG:初始:测试
02-29 23:26:53.338 18105-18105 / com.example.naveed.myapplication V / TAG:保存在preFS:TEST2
如您最初看到这是mDefault,然后测试得救了。我发起了一个新的活动,就回来了。下一次,最初的是试验,因为它保存最后一次和测试2被新的值保存。
I am just trying to store the users input from an editText in a Shared Preference, but it is not working:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int keycode, KeyEvent event) {
Log.v(TAG, keyword.getString("keyword", "mDefault")); //IT LOGS OUT THE DEFAULT STRING EVEN **AFTER** STORING THE PREFERENCES BEFORE
if (keycode == EditorInfo.IME_ACTION_SEND) {
editText.setText(editText.getText().toString());
keywordEditor.putString("keyword", editText.getText().toString());
keywordEditor.commit();
Log.v(TAG, keyword.getString("keyword", "default")); //CORRECT! THIS LINE WORKS
}
}
return true;
});
When I first edit the text, I will first get a log of "mDefault
" which is normal, since nothing is stored in the shared preference.
Then, I store something in the shared preference, and to make sure it stored, I log and I get a log of what I typed. Which means the shared preference data WAS stored.
Heres the problem: After I have stored something in the shared preference, I go to a different activity, and I come back, and all the data stored in the shared preference is GONE!
The very first log still says mDefault
after navigating through activities.
What could the problem be?
EDIT:
Here is my instantiation:
onCreate
:
keyword = PreferenceManager.getDefaultSharedPreferences(this); //Making a shared preferences
keywordEditor = keyword.edit();
Adding this as an example in case you missed a key components. This is currently working for me:
public class Main2Activity extends ActionBarActivity {
private SharedPreferences keyword;
private SharedPreferences.Editor keywordEditor;
private String TAG = "TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
keyword = PreferenceManager.getDefaultSharedPreferences(this); //Making a shared preferences
keywordEditor = keyword.edit();
final EditText editText = (EditText) findViewById(R.id.et_text);
findViewById(R.id.btn_launch).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, Main22Activity.class);
startActivity(intent);
}
});
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int keycode, KeyEvent event) {
Log.v(TAG, "Initial: " + keyword.getString("keyword", "mDefault")); //IT LOGS OUT THE DEFAULT STRING EVEN **AFTER** STORING THE PREFERENCES BEFORE
if (keycode == EditorInfo.IME_ACTION_SEND) {
editText.setText(editText.getText().toString());
keywordEditor.putString("keyword", editText.getText().toString());
keywordEditor.commit();
Log.v(TAG, "Saving in prefs: " + keyword.getString("keyword", "default")); //CORRECT! THIS LINE WORKS
}
return true;
}
});
}
}
From a fresh install:
I typed in "test" and hit the send button on keyboard therefore invoked the onEditorAction
Then clicked on launch new activity -> hit back button and typed in "test2" and hit send button.
The following is the log out put:
02-29 23:26:42.068 18105-18105/com.example.naveed.myapplication V/TAG: Initial: mDefault
02-29 23:26:42.136 18105-18105/com.example.naveed.myapplication V/TAG: Saving in prefs: test
02-29 23:26:53.281 18105-18105/com.example.naveed.myapplication V/TAG: Initial: test
02-29 23:26:53.338 18105-18105/com.example.naveed.myapplication V/TAG: Saving in prefs: test2
As you can see initially it was "mDefault" then "test" was saved. I launched a new activity and came back. Next time the initial was "test" since it was saved last time and "test2" was the new value saved.
这篇关于共享preferences? (非常简单的问题!?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!