Refreshing an ExpandableListView from within the adapter on Android - 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.

Related

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.

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?

How to access UI elements created by a List Adapter from an Activity?

In my code now, I have a DetailActivity, which simply calls a ListAdapter class to populate a ListView.
Within the ListAdapter class, I am inflating some custom views, some of which contain Buttons.
Back from within my DetailActivity, I would like to be able to access these buttons to enable/disable them depending on certain user actions. Is there a way to do this?
I guess the larger question is: from an Activity, how can I grab a reference to any element (buttons, imageviews, textviews,etc) that are created from an Adapter?
Thank you!
I assume you have a List<Object> that is sent through the constructor of ListAdapter.
Just add a boolean isEnable to the Object, and then in your getView() method, add this line:
button.setEnabled(getItem(position).isEnable);
In your DetailActivity, you can changeisEnable as you wish. And remember to adapter.notifyDataSetChanged() to get it worked.

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.

How to reuse one custom listview activity in another activity?

Can I use my existing ListView as a sub-view in another view.
eg. I have a custom view, players list, I have implemented with a ListActivity and ArrayAdapter and is working fine.
Now I want a way to get this listview as View object so that I can add this view object as a child to another view.
I am thinking like, to build entire listview: my ListActivity is calling the ArrayAdapter iteratively by passing ArrayList item each time to build a list item view.
If I am correct, I need a way to call the same ArrayAdapter and need to prepare a ListView Object, right?
Can anybody plz help me.
Thanks in advance.
vpapana
The ListView and the Adapter are two different things:
The data is somewhere (in an array or if you need to be able to add/remove items, in an ArrayList)
The Adapter contains the data
The ListView displays it.
So:
Construct your empty ArrayList
Construct your adapter based on your ArrayList
Construct your ListView based on the Adapter
Then:
Each time you need to change the data, update the ArrayList
Call Adapter.notifyDataSetChanged()
Watch your list being automatically updated :)
To add your list to a tablelayout, see the API documentation, SDK tutorial and this tutorial which has a specific section on how to add rows programmatically.

Categories

Resources