Force ExpandableListView to update data when notifyDataSetChanged is called
I have an ExpandableListView and an ExpandableListAdapter. When I call notifyDataSetChanged()
in my adapter, the data is not refreshed until I collapse and then expand the group. Here's the code in question.
First, my activity (the relevant parts)
public class HomeActivity extends ExpandableListActivity {
Building of the adapter and list
homeList = (ExpandableListView)this.findViewById(android.R.id.list);
homeAdapter = new ExpandableHomeAdapter(this, profile.Statements, getItems(itemCount));
homeList.setAdapter(homeAdapter);
homeList.setIndicatorBounds(0,0);
homeList.setOnChildClickListener(this);
homeList.expandGroup(0);
homeList.expandGroup(1);
In this activity, I have an event that the activity is registered for that lets it know the model data (stored in the application controller), has changed. Here is that event.
private DataChangedListener dataChanged = new DataChangedListener() {
@Override
public void dataChanged(DataChangedEvent event, ChangeModel changes) {
profile = Controller.getProfile();
for (int x = 0; x < changes.NewItems.length; x++) {
homeAdapter.insertItem(changes.NewItems[x], x);
}
for (int x = 0; x < changes.NewStatements.length; x++) {
homeAdapter.insertStatement(changes.NewStatements[x], x);
}
}
};
And then there is my adapter
public class ExpandableHomeAdapter extends BaseExpandableListAdapter {
The insert method
public void insertItem(ItemSummary item, int index) {
items.add(index, item);
this.notifyDataSetChanged();
}
This same approach using the notifyDataSetChanged()
worked when I was using multiple ArrayAdapter
s. Now, however, while everything works and the data is in fact changed, the screen does not update with the new data until I (manually) collapse and then expand the group. I haven't tried programatically collapsing/expanding but I'd like to save that as a last resort. How do I开发者_JS百科 get the view to update when the data changes?
I think this is happening because of the way in which you're inflating the ExpandableListView
. Try this: Instead of doing a findViewById
to grab the ELV, call the setContentView
of the layout that contains the ExpandableListView
. Then call:
ExpandableListView homeList = getExpandableListView();
Also, is there a way for you to call the notifyDataSetChanged()
method from outside the class. Maybe you could call it from the same place you call the InsertItem
method.
精彩评论