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
Related
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
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
In my application I am fetching the data from a web service in XML format, and parsing it and showing the data in listview. The problem is that if the web service contains 5000 objects then it takes a lot of time to display the data.
Can it be possible to show some data in listview and fetch the data at the same time at the end of the list.
Please provide me some sample code.
If you use convertView in your ListAdapter´s getView method it should not matter how many items you have in the list since the Views are beeing reused.
If your Listadapter takes an array of som sort you could add items to the array continuosly and call
mListAdapter.notifyDataSetChanged();
every time new data is added to the list.
By Using AsyncTask you can do this easily as each object is being fetched can be shown in listview using publishProgress() method while also updating user about what percentage of data hasbeen loaded.
Update:
By the way according to your situation the tool below which is developed by commonsware https://stackoverflow.com/users/115145/commonsware will suits you best...
https://github.com/commonsguy/cwac-endless
cwac-endless: Provides the EndlessAdapter, a wrapper for an existing ListAdapter that adds "endless list" capability. When the user scrolls to the bottom of the list, if there is more data for this list to be retrieved, your code gets invoked in a background thread to fetch the new rows, which then get seamlessly attached to the bottom of the list.
I have an ArrayAdapter that's using an ArrayList to display data in a ListView.
During the course of the activity, I sometimes need to edit the ArrayList by adding and deleting items.
Is there a difference if I call the add/delete functions on the actual ArrayAdapter vs. the underlying ArrayList? Which is better to use?
Use the adapter methods. This will automatically notify your adapter (and thus the bound list) that your data has changed.
Some times it is necessary (or at least more convenient) to modify the ArrayList (e.g., is a field of some other class, or it is being modified by other thread that does not know about the adapter).
In those cases, you will need to call adapter.notifyDataSetChanged()
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.