调用notifyDataSetChanged时强制ExpandableListView更新数据
我有一个 ExpandableListView 和一个 ExpandableListAdapter。当我在适配器中调用 notifyDataSetChanged()
时,数据不会刷新,直到我折叠然后展开该组。这是有问题的代码。
首先,我的活动(相关部分)
public class HomeActivity extends ExpandableListActivity {
适配器和列表的构建
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);
在此活动中,我有一个活动注册的事件,让它知道模型数据(存储在应用程序控制器中)已更改。这是那个事件。
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);
}
}
};
然后是我的适配器
public class ExpandableHomeAdapter extends BaseExpandableListAdapter {
插入方法
public void insertItem(ItemSummary item, int index) {
items.add(index, item);
this.notifyDataSetChanged();
}
当我使用多个ArrayAdapter 时,使用notifyDataSetChanged() 的相同方法也有效。然而,现在,虽然一切正常并且数据实际上已更改,但屏幕不会使用新数据更新,直到我(手动)折叠然后展开该组。我还没有尝试过以编程方式折叠/展开,但我想将其保存为最后的手段。当数据发生变化时,如何让视图更新?
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 get the view to update when the data changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为发生这种情况是因为您膨胀
ExpandableListView
的方式。试试这个:不要执行findViewById
来获取 ELV,而是调用包含ExpandableListView
的布局的setContentView
。然后调用:另外,有没有办法从类外部调用
notifyDataSetChanged()
方法。也许您可以从调用InsertItem
方法的同一位置调用它。I think this is happening because of the way in which you're inflating the
ExpandableListView
. Try this: Instead of doing afindViewById
to grab the ELV, call thesetContentView
of the layout that contains theExpandableListView
. Then call: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 theInsertItem
method.