0

I have a database in my android app which contain 2 fields named "word" and "meaning".From this database, i can retrieve data(word,meaning) and then i have to save them to shared preference and these saved shared preferences will be showed in a listview. These are my goals.

Now,only I can retrieve data from the database and can show them into a list view.For this my codes were:

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

            String[] from = { "key_word","key_meaning" };
            int[] to = { R.id.txt1,R.id.txt2};           
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_wordmeanings, R.layout.list_layout, from, to);
           listView.setAdapter(adapter);

I also can save data into shared preference like this:

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();

But i dont understand how to save these two fields(word,meaning) into a list of shared preference and show them(the shared preference list) into a listview. Can anyone help me how to do this???

ImonBayazid
  • 1,066
  • 3
  • 16
  • 41
  • Did you check this [How to save an ArrayList in the SharedPreferences](http://stackoverflow.com/a/10815983/1403997). – 4gus71n Oct 13 '13 at 20:03
  • I dont understand how to show shared preference list into a list view ??Could you help me please ? @astinx – ImonBayazid Oct 14 '13 at 04:06
  • 1
    Naah..Its better to fire a Query rather than saving a list in SharedPrefs. Its a general topic 'SQLite Vs. SharedPrefs' and as far as I've experienced, SharedPrefs are meant for small datas like an int, string or bool value. For a whole list, go ahead with firing the queries! – Prachi Oct 14 '13 at 06:36

1 Answers1

0

In your case, it's better to save the list in the database.

Shared preferences are organised by keys. Each key can have a value. The problem with saving lists or arrays in shared preferences is that there's no good way of storing the key which would have to consist of some general identifier plus each item's identifier. It's not convenient for storing and retrieval.

There is also a performance consideration: databases are better for storing and retrieving values than xml files (shared preferences are just an xml file).

Szymon
  • 42,577
  • 16
  • 96
  • 114