Android:在共享首选项中动态存储字符串数组
我的应用程序中只有一个edittext,我想在单击按钮时从中存储字符串数组。就像任何浏览器的历史记录一样。我想在应用程序启动时显示存储的字符串。我一直在尝试使用单一文本视图的共享首选项。我已附上我的活动代码Android:在共享首选项中动态存储字符串数组,android,sharedpreferences,Android,Sharedpreferences,我的应用程序中只有一个edittext,我想在单击按钮时从中存储字符串数组。就像任何浏览器的历史记录一样。我想在应用程序启动时显示存储的字符串。我一直在尝试使用单一文本视图的共享首选项。我已附上我的活动代码 public class MainActivity extends Activity { Button insert; EditText edt; TextView txt1, txt2, txt3, txt4, txt5; @Override public void onCreate(
public class MainActivity extends Activity {
Button insert;
EditText edt;
TextView txt1, txt2, txt3, txt4, txt5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt = (EditText) findViewById(R.id.search_word);
txt1 = (TextView) findViewById(R.id.txt1);
insert = (Button) findViewById(R.id.insert);
insert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
String text = edt.getText().toString();
editor.putString("key", text);
editor.commit();
Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT)
.show();
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(this);
String text = app_preferences.getString("key", "null");
txt1.setText(text);
}
}
我唯一的一个文本视图是填充,我想用它显示最近五次搜索历史。请告诉我一些有关这方面的想法。序列化您的数据。数组->字符串和字符串可以保存到首选项,可以使用JSON对数据进行编码/解码。 http://www.json.org/到底是什么问题?“我有问题”并不能真正帮助理解发生了什么