0

I have a Google's NavigationDrawer which adds/replaces fragments. One of fragments has a listView filled with custom BaseAdapter by network request (asyncTask, takes times). What i want is to save listView contents somewhere so if user navigates to another fragment through navigationDrawer and then or later navigates back to the fragment containing listView - i want a listView to be populated immediately with saved old content before asyncTask finished loading new content. Minimum API is 10.

What did i try.

  1. onSaveInstanceState - serialize Parcelable ArrayList<CustomObject>. Somehow i didn't get it working. Also, that isn't solving my problem however, because onSaveInstaceState doesn't triggers on navigating through navigationDrawer.

  2. Setting new fragment's InitialState(setInitialSavedState) then saving(saveFragmentInstanceSate)/loading it. That works for simple Views like EditTexts and TextView, but didn't get it working for the listView.

What is a best way to save listView contents? Please help me.

localhost
  • 5,568
  • 1
  • 33
  • 53

3 Answers3

1

First get all items of list view.

CustomListViewAdapter listadapter = (CustomListViewAdapter) listview.getAdapter();
ArrayList<CustomObject> object=new ArrayList<CustomObject>();
for(int position=0;position<listadapter.getCount();position++)
    object.add(videoadapter.getItem(position));

Now Use the object to store the items of the listview

Then use shared preferences to save the object. Android ArrayList of custom objects - Save to SharedPreferences - Serializable?

Community
  • 1
  • 1
TechArcSri
  • 1,982
  • 1
  • 13
  • 20
  • I have no problem to store content in a variable. But after user navigates off fragment - it get destroyed along vith variables. – localhost Feb 18 '14 at 07:03
  • if this is helped u.. then atleast upvote me or accept the answer – TechArcSri Feb 19 '14 at 07:58
  • Serializing with gson - slow. Serializing with ObjectSerializer is not acceptable for ArrayList, so i still look for better solution. – localhost Feb 19 '14 at 08:26
0

The proper way to do this is to save your network query results in a database (sqlite), and use data from that db to display items in your list (CursorAdapter works best for this).

These tutorials nicely explain how to make your own Content Provider using Sqlite, and use a CursorAdapter to display your data on a list.

http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_4_-_using_cursoradapters/

http://www.vogella.com/tutorials/AndroidSQLite/article.html

josephus
  • 8,284
  • 1
  • 37
  • 57
0

I found a good way.

    String list_items = "";   // all your items are here,               but separate it with a comma (,)
    String[] list = list_items.separate(",");

And save the list_items in a shared preference. To retrieve just use getString() and use the code above

Devam03
  • 141
  • 1
  • 7