我正在做的是从AlertDialog条目填充一个ListView。我试着用SharedPreferences来做到这一点,以前我已经把最后一个条目保存到ListView中(如果我输入了多个String),所以当我离开活动并返回列表中出现的所有内容时,我最后一次条目。从SharedPreferences填充ListView
但是我想出了这个问题,并修改了保存过程,通过增加一个附加的Int变量来更改存储的首选项的“Key”字段。
为了让你发生了什么事的全貌: 第一篇文章这么PIC是在这里:http://imgur.com/yGLQx 这是多个条目的输出到ListView。
如果你仍然在活动中,ListView会填充来自用户的条目,但是在离开活动并返回给它之后,它就像条目已经被注册,但是String值没有被保存。
这里是我的代码:
public class ManageLinguisticPhrases extends ListActivity {
private static final String PHRASE = "Phrase_";
private SharedPreferences prefs;
private String prefName = "myPhrasesStorage";
ArrayList <String> listItems = new ArrayList <String>();
ArrayAdapter <String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
LinkedList<String> phrasesCollection = new LinkedList<String>();
int phraseCount = prefs.getInt("phrase_count", 0);
for(int i = 1; i <= phraseCount; i++) {
phrasesCollection.add(prefs.getString(PHRASE + i, "<< Enter a phrase >>"));
}
listItems.addAll(phrasesCollection);
adapter.notifyDataSetChanged();
}
@Override
protected Dialog onCreateDialog(int id)
{
final EditText input = new EditText(this);
final String text = input.getText().toString();
switch (id)
{
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Please enter the new data")
.setView(input)
.setPositiveButton("Save", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
listItems.add(input.getText().toString());
adapter.notifyDataSetChanged();
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//increments index by 1
editor.putInt("phrase_count", prefs.getInt("phrase_count", 0) + 1);
//save new phrase in myPhrasesStorage with key "name[index]"
editor.putString(PHRASE + (prefs.getInt("phrase_count", 0) + 1), text);
editor.commit();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
}
})
.create();
}
return null;
}
private void CreateMenu(Menu menu)
{
MenuItem mnu1 = menu.add(0, 0, 0, "Add data");
{
mnu1.setAlphabeticShortcut('a');
mnu1.setIcon(android.R.drawable.ic_menu_add);
}
MenuItem mnu2 = menu.add(0, 1, 1, "Edit data");
{
mnu2.setAlphabeticShortcut('b');
mnu2.setIcon(android.R.drawable.ic_menu_edit);
}
MenuItem mnu3 = menu.add(0, 2, 2, "Delete data");
{
mnu3.setAlphabeticShortcut('c');
mnu3.setIcon(android.R.drawable.ic_menu_delete);
}
}
private boolean MenuChoice(MenuItem item)
{
switch (item.getItemId())
{
case 0:
showDialog(0);
return true;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
return MenuChoice(item);
}
所以呀看看,告诉我,如果我做的事情很可笑,让我知道!
欢呼声!
问题也可能与AlertDialog中'ArrayList listItems'的手动输入有关... – 2012-03-11 12:59:38