0

我有按钮,单击按钮后它将列出到 ListView,选择 ListView 项目,该项目将显示在按钮的左侧,我希望在我重新启动程序后显示选择的 listItem。如果有人知道这个,请帮助我?谢谢

4

1 回答 1

0

有两种可能的简单方法:

  1. SharedPreference(最简单)
  2. SQLite(也许对你的目的没用)

如果您必须只保存从微调器中选择的文本,那么 SharedPreference 可能是解决方案。这是如何使用它:

从您的 PREFERENCES_LIST 读取值

// set name of your preferences list
private static String MY_PREFERENCES = "my_preferences_list";

// set key for retrieving text you saved
private static String TEXT_DATA_KEY = "last_text_spinner_choise";


// create a pointer to your preferences list specifying name and access typology
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);

// get String from preferences list specifying KEY of Text you want retrieve and a default string if key doesn't exist
String textData = prefs.getString(TEXT_DATA_KEY, "No Preferences!");

// your textView or something else
TextView outputView = (TextView) findViewById(R.id.outputData); 

// set textView with value taken from your preference list
outputView.setText(textData); 

在您的 PREFERENCES_LIST 中写入值

// Same as before
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
// Editor pointer for changing value in your preference list
SharedPreferences.Editor editor = prefs.edit();

// your editText or spinner from which you want take text value
EditText outputView = (EditText) findViewById(R.id.inputData);
CharSequence textData = outputView.getText();

if (textData != null) {

   // Here you save textData in your preferences_list. You have to specify key and value. Key is important for retrieving text as seen before
   editor.putString(TEXT_DATA_KEY, textData.toString());
   // Confirm your choice by commit()
   editor.commit();
}

希望能帮助到你

于 2012-11-29T09:38:56.907 回答