How to force my GridView adapter to reload? - android

I'm an Android beginner starting with fragments. My problem is the following.
I has a fragment that used to show a custom listview (it extended SherlockListFragment). I populated the listview with some data server. The action of retrieveing data of the server was executed in another thread and I had a handler to manage responses. In this handler (inside my fragment) I recovered data for my adapter and the I reloaded my adapter like this ((myAdapter) fragment.getListAdapter()).dataChange(); .
Well, now I wanted to convert the list in a custom grid cause I think it's much better for my app. So, the fragment doesn't extends SherlockListFragment, but BaseFragment. As I don't have a ListView anymore, I can't not use "getListAdapter.datachange()". Does anybody know the correct way to do this?
Thanks in advance.

Make an Adapter for the gridview that extends from BaseAdapter, or use an Adapter that extends from BaseAdapter.
then call notifyDataSetChanged on that adapter when the contents of your collection has changed.
Small example of adapter and grid

I would recommend making your changes on the Adapter, not the Grid (or list). Depending on the type of adapter you are using there are a couple different approaches.
ArrayAdapter
For an ArrayAdapter, using the standard modification methods (add, addAll, clear, remove) should update any views.
CursorAdapter
A CursorAdapter works in much the same way, except that you use either swapCursor or changeCursor to change the underlying data. That should notify all data set observers that the underlying data has changed.
All Adapters
If your observers don't get notified, usually because setNotifyOnChange was called, you can call mAdapter.notifyDataSetChanged() to manually update all listeners (UI views, services, etc.) that the underlying data has changed.

thanks a lot for your quick responses. I tried your ideas but finally I converted my old list in a grid by making a two columns' row. It was the best option to keep my header and footer on.

Related

Is it okay to do the RecyclerView setAdapter() several times?

When use RecyclerView, Does it matter how many times setAdapter() has been used?
Or should setAdapter be used only once?
and Is it okay to use setAdapter after adding items to the adpater?
Or should the setAdapter work before adding items to the adapter?
General practise is to call RecyclerView::setAdapter once per instantiation of the RecyclerView then use the RecyclerView.Adapter<VH> for updating the underlying List<T> data set then calling methods like Adapter::notifyDatasetChanged.
Recyclerview.Adapter also allows updating individual rows through other methods : https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
More modern techniques include DiffUtil or AsyncListDiffer which uses DiffUtil or manual implementations to abstract working out individual row changes between data set updates. This is the most efficient mechanism as it only needs to "rebind" changed data on screen, rather than rebinding all views.
If you intend to change the type T of the underlying data set then you can call RecyclerView::setAdapter more than once, as you are fundamentally changing the adapter data set type. This however is an edge case.
Unless you are switching different adapters on the same RecyclerView, it's recommended you call setAdapter once (even with an empty list of elements). Then, when you update the list of elements, you can call adapter methods like notifyDataSetChanged and other similar methods.
Well, if you are setting the same adapter instance multiple times just to refresh then, please take a look at RecyclerView.Adapter#notifyDataSetChanged()
A better approach would be 'setAdapter work before adding items to the adapter' and then you can add, remove, and modify adapter data items. Then you can notify the adapter.

Listview fillup with adatper

I have created custom listview with baseadapter.
it's working fine.
but I am calculating total amout when adatper is filliup via getview method.
My question is when I can find that adapter has completed his process of filling up listview?
When adapter called getview() method for last the record after that I want to broadcast message. How can I do?
Thanks.
Adapter does not fill listview completely. It will only fill views that are on the screen ( it also depends on framework cos companies like Samsung, Sony do change the android framework a lot)
The best you should do is call Notifydatasetchanged on adapter and then handle call after that.
Views and whatever adapter needs to call should be done by then
Can you please explain more what you want to achieve. You probably need to use the adapter data and not think about touching its UI part?

Is it okay to change a ListView's adapter dynamically?

Instead of creating multiple activities, I would like to change the ArrayAdapter of the ListView as needed. I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.
To be more specific, say I would like to start an activity that has a ListView. In this example, the ListView is initialized with a listView.setAdapter(this) from, say, a CategoryArrayAdapter.
Then a user selects a category. Without starting a new activity, the code will set a new adapter for the same ListView. The new adapter, say ItemArrayAdapter calls listView.setAdapter(this).
Does someone have experience having done this successfully or know of a specific reason why this shouldn't be done?
I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.
The simple answer is YES, and I have done similar sort of things before.
This is exactly the reason why Adapter is existed and provided in the API. The actual content (Model) and how it is rendered (View) for each list items is isolated and implemented inside android.widget.Adapter, instead of directly bound to android.widget.AdapterView. As long as your adapter is properly implemented, you can swap/change the actual underlying adapter that bound to the ListView, simply by calling the setAdapter() method.
Resetting the adapter is ok, but notice, that there might be a GUI glitch when doing so, as the view whose adapter is being changed has to be redrawn with the new data. Aside from this you should be fine.

Refreshing an ExpandableListView from within the adapter on Android

I'm having trouble on how to refresh an ExpandableListView from within one of its childViews.
The listView contains a list of files, and I want to offer a possibility to delete those files. But after a deletion is performed, the ExpandableListView needs to be refreshed.
I implemented my own adapter which extends the BaseExpandableListAdapter. It seems there is no way to get my parent object from within the overwritten getChildView() method to send a notifyDataSetChanged(). :-s
I thought about sending an intent to my activity which contains information on the state of the groups (collapsed/expanded) and to refresh on receive, but even for this I would need the ExpandableListView object.
How can this be done?
Thanks in advance!
Be well
S.
You have to
remove the corresponding item from your adapter
and then call adapter.notifyDataSetChanged()
Then the ListView will reload the data.

How to delete checked items in a ListView

I want to have a Button that when clicked, removes all the checked items in the ListView. I already have all the xml items set up, I just don't know how to write the java code.
The ListView displays data that comes from an Adapter. In order to remove items from the view the item needs to be removed from the Adapter and the view notified. In android the Adapter notifies the view by calling notifyDataSetChanged().
How to remove an item from the adapter depends on your particular adapter. The SimpleCursorAdapter gets its data from an underlying Cursor. To remove an item, the item should be removed from the underlying Cursor. For example using a SQLiteCursor a row in the database needs to be deleted.
If you use the ArrayAdapter just call remove(T object) on the adapter. It will automagically call notifyDataSetChanged() for you.
update:
I saw the code at git hub. Here are some pointers as how to get your app working as soon as possible.
Try refactoring your code in to smaller graspable parts. Start with extracting some methods to give parts of the large method understandable names.
The problem is that there might by hundreds of rows in the database and only enough views to fill the screen. Nowhere is it remebered what rows are checked, hence its not possible to remove them. You probably need to extend BaseAdapter or SimpleCursorAdapter to hold the state (checked or not) of the rows. Read up on the excellent android documentation.
My point here is there is a distinction between the view, your CheckBox, and the model containing the data to display. So check out Model-View-Controller. You can ignore the concept of controller for now.

Categories

Resources