Save ListView content - android

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.
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.
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.

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?

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

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

Related

android clear arraylist or adapter?

After working on an app for a while I realize I use
adapter.clear()
and
arraylist.clear()
I can see both are working just fine, I would like to know the difference between the two!
Both are called before I start and asyncTask that updates my list with information from my server!
You should not be clearing the ArrayList directly. The ArrayAdapter makes absolutely no guarantees that it maintains the same referenced list given to it. In fact it will change when you perform a search with it's filter. Which would make arrayList.clear() fail.
Rule of thumb, if you ever need to mutate or retrieve the associating data...do it directly from the adapter. Not the list you used to construct it.
Adapter = it contains copies of diff views,arrays
aaraylist holds the data which we want to display in our view.
ex: arraylist<HashMap<String,String>> ah= new ArrayList<HashMap<String,String>>();
the above list contains hashmap
if i clear the arraylist there will be no data to show on listview or gridview so it will be empty
if i clear adapter than it will destroy the copies of array and views so the output will be same

Is there a simple list widget in android that lets me select from a list without having to create an adapter?

I have a list, I want the user to select one. It's a string. Maybe even have an Object I can associate with it.
Is there any way to do this without creating a subclass with an arrayadapter?
I see tons of examples and they all seem overengineered for what must be the most basic list handling problem in the world. Is there no default simple list string handler built in?
If there is, I can't find it.
Help?
Is there no default simple list string handler built in?
ListView is a simple list string handler that's built in.
Is there any way to do this without creating a subclass with an
arrayadapter?
You don't have to subclass ArrayAdapter to use it. Binding the data to ListView is one line of code:
final ListView list = ...;
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
In your case, data would either be a List<String> or a String[].

How to create custom dynamic list in android?

I want to create a custom list in which items are added or removed dynamically (say when a button is tapped). The problem is I have very little knowledge of lists in android. I have gone through various tutorials on creating a custom list in android but none of them shows how to dynamically add contents to it
What I know so far:
1) I have to create a model class to store data.
2) I have to create an adapter class.
3) Pass the objects of the model class as an arraylist to the adapter.
3) Bind listview to the adapter
What's confusing me:
1) I know I have to create an apapter class, but what's really confusing me is what kind of adapter ? i.e. ArrayAdapter, BaseAdapter ??
2) What and How will I feed adapter? I will be fetching data from the Sql lite database and I want the results to be displayed in my custom made list.
3) How will I update my list when a new record is added to the database ? I know how to populate listview from a static array but its of no use in my project.
I need little guidance where should I start from ?
1) You can use ArrayAdapter.
2) After you create your own arraylist, you can pass it for first time, listview.setAdapter(...
3) After you refresh your data you can call this method, ((ArrayAdapter)listView.getAdapter).notifyDataSetChanged(). This will ensure your listview refresh.
The below link is a good example:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

Usage of Android SimpleCursorAdapter and CursorLoader

I am new to Android and am trying to get my header round the SimpleCursorAdapter and CursorLoader classes. From my understanding, all of the examples that I have seen use these two classes to load data into a ListView in a background thread (to not block the UI).
I am OK with this and have used this general approach with my own list of items, however I now want to be able to click on an item in the list and get the full information for the item. Is it usual practice to use SimpleCursorAdapter and CursorLoader to retrieve the details for a single item? or are they just meant for lists?.
Thanks.
They are not meant for lists only. You can - and should - use them in detail views (activities) as well.
I've sketched a possible way to do so on my blog:
http://www.grokkingandroid.com/using-loaders-in-android/
Think of Adapters as a layer of abstraction between your data (Cursor) and whatever you attach that Adapter to (ListView for example). This way, you have a common interface between your data (Cursor, ArrayList, whatever) and the View you display that data on (ListView, TableView, etc.), this is helpful because if you later find that you want to access your data through an ArrayList rather than a Cursor, then you simply swap out the adapter with a different one and you're ready.
Now considering your question, Adapters give an abstract access to information, therefore you can "ask" it for what information is stored and where. You could attach an OnItemClickListener to your ListView and then access your data from there.

Passing information between views

I am new to android programming, but I am trying to learn. I have written some code that takes in some parameters through a "normal" view with checkboxes and textviews. Then I use this information to generate a lot of numbers that I want to display in a listview. I have managed to create a listview when I press a run button, but how do I pass the information from the main view to the listview. Is it best to pass the information one number at the time or a large array with all the numbers. The list of numbers can be really large.
What you probably what to do is create an adapter with the numbers as the data source. If the numbers are in an array you can create a new ArrayAdapter and set the ListView adapter as that adapter:
ArrayAdapter adapter = new ArrayAdapter<Double>(getApplicationContext(), R.id.id_of_textbox, arrayOfDoubles);
listView.setAdapter(adapter);
In this code I've assumed the numbers are doubles, however ArrayAdapter is a generic class so it can be any object contained in the array. The array can also be presented as a List (like an ArrayList).
Hope that helps you out. Here are some bit of documentation to read and some good video sessions to watch:
ArrayAdapter
ListView.setAdapter()
The World of ListView Google I/O 2010 Session
How big is the array can get?
Most likely that displaying the list as another activity and passing the data as intent's extra will be the solution.

Categories

Resources