Android app: Update UI of some item views in a GridView - android

I have an Activity that hosts a GridView. When the Activity is resumed, in the OnResume method, I want to update the UI of some item views (but not all) of the GridView because their underlying data has changed. I know that the GridView has the invalidateViews() method, but that is overkill because it recreates all views. In my case, I just want to update some views.
Is there an efficient way to achieve this?
Thanks

After changing the data of gridview call adpter with notifydatachanged() after that only it update the gridview with fresh dara
adapter.notifyDataSetChanged();
If you use invalidate views it will create view again

Related

How to update a single view of listview instead of notifyDataSetChanged in android

I want to update single row of listview which contains a image after certain time of addition to listview. I am using notifyDataSetChanged for this but when other visible views contains a imageView, it gets a jerk felling on other visible imageViews due to notifyDataSetChanged.
Can someone suggest a better way to do it.
Thanks
Keep a reference to the imageview when you create/update it in the adapter. You can then change the image later on. However, since list items get recycled, you will want to set a tag on it with some id of the content so that you make sure you're still updating the right item.

Should i use notifyDataSetChanged or update ListView items individually?

I've got a question about the performance of a ListView. My application uses a ListView with approximately 20 items and i was wondering what should i do if the data of one item has changed. Should i call notifyDataSetChanged() on the adapter and cause a redraw of the whole list or should i take care only of the item to refresh itself?
What is the cost of notifyDataSetChanged()? Can i use this without hesitation? An ListView item of mine has about 3-4 TextViews and an ImageView.
Any suggestions?
notifyDatasetChanged refreshes the whole list each time its called so it is better if you call that after you have all of your data that you want so you are not doing unnecessary work
mMyListView.invalidate();
mMyListView.invalidateViews();

How to force my GridView adapter to reload?

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.

How to force redraw of android UI?

When we initialise adapters, we have method getView. As I understand, each time it is called, It redraws the UI. Is there other ways to redraw UI?
calling adapater.notifyDataSetChanged()
From the documentation
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.
notifyDataSetChanged() will redraw all views in your adapter.
A neat trick if you want to redraw only one view from your adapter is to call getView manually for it if its position is visible - i.e. when you have multiple rows and each one updates independently from the others. In such a scenario, it would be a waste to call notifyDataSetChanged every time a single row gets updated.
you can use notifyDataSetChanged() method for adapter.
Which is called as adapter.notifyDataSetChanged();
Which informs Adapter that background task is completed and now the data set is changed.
you can refer this

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.

Categories

Resources