0

我从数据库中检索 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 个键和值。现在我必须将这些键和相应的值显示到列表视图中。我怎样才能做到这一点??

4

1 回答 1

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();

         }

然后将此数组列表传递给您的列表视图适配器...

于 2013-10-14T05:13:09.063 回答