我从数据库中检索了5个数据(有2个列字和含义)并将它们保存到共享首选项中。所以我有共享偏好的5项。现在我如何将它们显示在列表视图中。我尝试了很多次,但无法解决这个问题。 我的代码是:
ArrayList<HashMap<String, String>> list_of_wordmeanings = new ArrayList<HashMap<String, String>>();
Cursor mCursor = mDbHelper.learn_getTestData();
for (int i = 0; i < mCursor.getCount(); i++)
{
mCursor.moveToPosition(i);
String string = mCursor.getString(1);
HashMap<String, String> hm = new HashMap<String, String>();
String word = mCursor.getString(0).toString();
String meaning = mCursor.getString(1).toString();
hm.put("key_word",word);
hm.put("key_meaning",meaning);
list_of_wordmeanings.add(hm);
}
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for (HashMap<String, String> map : list_of_wordmeanings)
for (Entry<String, String> entry : map.entrySet())
{
editor.putString(entry.getKey(), entry.getValue());
editor.commit();
String savedPref = sharedPreferences.getString(entry.getKey(), "");
Toast.makeText(getApplicationContext(),savedPref, Toast.LENGTH_LONG).show();
}
它向Toast提供5个键和值。 现在我必须将这些键和各自的值显示在列表视图中。 我怎样才能做到这一点??
答案 0 :(得分:0)
ArrayList<String> result = new ArrayList<String>();
for (Entry<String, String> entry : map.entrySet())
{
editor.putString(entry.getKey(), entry.getValue());
editor.commit();
String savedPref = sharedPreferences.getString(entry.getKey(), "");
result.add(savedPref);
Toast.makeText(getApplicationContext(),savedPref, Toast.LENGTH_LONG).show();
}
然后将此数组列表传递给listview适配器...