I have an Activity with a Fragment that displays a ListView containing simple TextViews. A menu item can trigger another Activity via an Intent. That new Activity clears the ArrayList underlying the ArrayAdapter for the ListView using ArrayList.clear().
When I backup from the new Activity to my original one with the ListView, and get control in onResume(), I find that my ListView.getChildCount() is the same as when it was left due to the Intent, but the ListView.getCount() is now properly zero!
I have tried using the adapter's clear() method, I have tried Adapter.notifyDataSetChanged() (although I should not have to).
If I modify the underlying ArrayList from within the fragment itself, all seems fine. For example, clicking on an element gives you an option to remove it, move it up or down, etc... That works OK.
Also, If I then leave the List Activity and return to it again, all is well. So clearly the ArrayList is the same list. I never create a new list, only .clear() it.
Any idea how the Child Count can possibly be more than the underlying element count? Perhaps some kind of observer for the ArrayList does not trigger because the Activity is suspended? In which case how could I sync them up again? I have tried invalidate() for example.
This is under API 23.
I seem to have found a workaround. I noticed another post here discussing thread safety. It seemed to have implications if you do not modify your list from the original UI. So, in my onResume(), I just did a setAdapter() again. With the same adapter and list, and viola! All seems well again.
Related
If I have a ListView with a CustomAdapter and let's say I have this refresh() method that refreshes the list with new results, should I:
Call new CustomAdapter(...) upon initialization, and every time I call refresh(), I use adapter.clear() and adapter.add(...)
Call new CustomAdapter(...) every time I call refresh()
Basically, I'm asking, is it better to recreate an adapter everytime I load new results or is it better to clear the results in existing adapter and add the entire list to it?
I think if you're going to use adapters as they were intended, then you'll update the adapter's data and call notifyDataSetChanged() on it.
If you look at the API for something like ArrayAdapter, it has numerous methods like clear(), add(), addAll(), notifyDataSetChanged() and so forth that are there to guide you towards a certain use of the API.
By using these methods, not only does it make your code compliant with its intended usage, it also makes it easier to understand and familiar to others that are trying to understand your code.
So basically, I would only recreate the adapter as a last resort.
It is definitely better to call notifyDataSetChanged() on the original adapter than setting a new one.
The reason is performance: ListView uses view recycling to avoid creating new item views as you scroll. When you set a new adapter, these recycled views are discarded, which means they must be recreated from scratch for the next layout pass. Take a look at the code of ListView.setAdapter():
#Override
public void setAdapter(ListAdapter adapter) {
if (mAdapter != null && mDataSetObserver != null) {
mAdapter.unregisterDataSetObserver(mDataSetObserver);
}
resetList();
mRecycler.clear();
...
This is completely logical behavior, since the ListView supposes that the views the new adapter will use are incompatible with the ones returned by the previous adapter (in any case, it cannot assume that they will be compatible). So they're thrown away.
Therefore, if you set a new adapter each time, you're incurring an unnecessary performance cost (recreating all the current views).
Also, if you wrote a custom adapter, you don't necessarily have to call add() individually (as with, say, ArrayAdapter). You can just replace the internal data collection with the new one and call notifyDataSetChanged() afterwards.
My project uses the SQLiteCursorLoader library from commonsguy to load data from a database into a ListView. Among that data is a simple boolean (as so far as SQLite supports booleans... that is, a number that only ever is 0 or 1) that tells the state of a checkbox. If I change the state of a checkbox in a list and then scroll the item off the list, the list item returns to the state it has when the cursor was passed in, despite the fact that the underlying database has changed. If I change the state of a bunch of checkboxes and then activate the list's MultiChoiceMode, all the items displayed will revert back to the state they were in when the cursor was originally passed in, despite the fact that the underlying database has changed.
Is there a way to refresh the cursor? Cursor.requery() is deprecated, and I don't want to have to create a new Cursor each time a checkbox is checked, which happens a lot. I'm also unsure of how calling restartLoader() several times would work, performance-wise, especially since I use onLoadFinish() to perform some animations.
Is there a way to refresh the cursor?
Call restartLoader() to have it reload the data.
I don't want to have to create a new Cursor each time a checkbox is checked, which happens a lot
Used properly, a ListView maintains the checked state for you. You would do this via android:choiceMode="multipleChoice" and row Views that implement Checkable. The objective is to not modify the database until the user has indicated they are done messing with the checklist by one means or another (e.g., "Save" action bar item).
I'm also unsure of how calling restartLoader() several times would work, performance-wise, especially since I use onLoadFinish() to perform some animations.
Don't call it several times. Ideally, call it zero times, by not updating the database until the user is done, at which point you are probably transitioning to some other portion of your UI. Or, call it just once per requested database I/O (e.g., at the point of clicking "Save"). Or, switch from using a CursorAdapter to something else, such as an ArrayAdapter on a set of POJOs you populate from the Cursor, so that the in-memory representation is modifiable in addition to being separately persistable.
my problem is the next one:
I want to use a ListActivity with recursion, so that it is called "x" times, and everytime i click in one article on the list, it is re-calling again the same Activity but it is loading different data, cause what I wanna do is to make menu and sub-menus, and the expandableList is not enough for me because there are gonna be n-levels(i will know dinamically.....).
Anyone has an idea how can I implement it??
Thanks!
Store every data in some kind of N level ArrayList. Write your Adapter class to accept a level of this list. On the item click, it passes to the next level of your ArrayList and you call a notifyDataSetChanged() on your list.
I've just a little question (I hope it's just a little question ;-)).
My current app-project has, of course, a bunch of activities. In one of these activities (I call it DataActivity) I can enter some informationen (stored on a webserver). Another activity (called ListViewActivity) should show all available information stored onto the webserver.
The problem is: My first step is to open the ListViewActivity with a few entries (it works proper at first time). Then I go to the DataActivity to input a new listitem. When I now go back to my ListViewActivity the new entry isn't listed. I think I have to refresh the ListViewActivity ... but I don't exactly know how. ;-)
What can I do to refresh this ListView after adding the new row/listitem, so that all available information will be displayed?
I assume you are calling the init of the list in onCreate. Just implement onResume (or onStart should work as well) and add a refresh() method that recalls the data from your server for your list.
You need to call notifyDataSetChanged() or notifyDataSetInvalidated() of your list adapter when you are back to your ListViewActivity.
I identified a problem in changing one activity using tab. In one tab activity I'm adding data to my SQLite database, and in the other tab activity I am displaying them using listview(array adapter). But when I come back to add data after adding new items to SQLite, the newly added records are not updated in my listview.
How do I fix this?
You seem to be pulling the list data from a DB. Is there a reason why you are using an ArrayAdapter instead of a CursorAdapter?
Anyway, you should call notifyDataSetChanged() on your list adapter when the data has changed so it can refresh the view.
you can add code to update your listview (via notifyDataSetChanged or some such) by overriding the onResume() method in your activity which is called whenever the activity is brought back to the foreground.
See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle