我是Android新手,我有arraylist存储来自用户输入的数据,我希望在用户关闭应用程序时保存该数据,并在用户再次打开应用程序时检索它 这是我的代码
String getInput= inputText.getText().toString();
if(addArray.contains(getInput) || getInput.equals("")){
Toast.makeText(getBaseContext(), "Item Already Added or Cannot Be Empty", Toast.LENGTH_LONG).show();
}else{
addArray.add(getInput);
Toast.makeText(getBaseContext(), "Item Added", Toast.LENGTH_LONG).show();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice,addArray);
show = (ListView)findViewById(R.id.arrayList);
show.setAdapter(adapter);
((EditText)findViewById(R.id.edtInput)).setText(" ");
答案 0 :(得分:0)
您可以使用PreferenceManager;
以下是保存密码并在应用程序打开时获取密码的示例
public class SavedPreference
{
static final String PREF_USER_NAME = "username";
static final String PREF_PASS = "password";
static SharedPreferences getSharedPreferences(Context ct)
{
return PreferenceManager.getDefaultSharedPreferences(ct);
}
public static void setUserName(Context ctx, String userName)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_USER_NAME, userName);
editor.commit();
}
public static void eraseSavedPreference(Context ctx)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.clear();
editor.commit();
}
public static String getUserName(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}
}
如果要在应用程序执行时检查某个字符串是否已保存
if(SavedPreference.getUserName(this).length() == 0)
{
//string not found
}else
{
//string found
}