gpt4 book ai didi

android - 调用 notifyDataSetChanged 时强制 ExpandableListView 更新数据

转载 作者:行者123 更新时间:2023-11-30 04:38:44 25 4
gpt4 key购买 nike

我有一个 ExpandableListView 和一个 ExpandableListAdapter。当我在我的适配器中调用 notifyDataSetChanged() 时,数据不会刷新,直到我折叠然后展开该组。这是有问题的代码。

首先,我的 Activity (相关部分)

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);

在这个 Activity 中,我有一个 Activity 注册的事件,让它知道模型数据(存储在应用程序 Controller 中)已更改。这是那个事件。

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() 的方法同样有效。然而,现在虽然一切正常并且数据实际上已更改,但在我(手动)折叠然后展开组之前,屏幕不会更新新数据。我没有尝试以编程方式折叠/展开,但我想将其保存为最后的手段。如何在数据更改时更新 View ?

最佳答案

我认为发生这种情况是因为您膨胀 ExpandableListView 的方式。试试这个:不要执行 findViewById 来获取 ELV,而是调用包含 ExpandableListView 的布局的 setContentView。然后调用:

 ExpandableListView homeList = getExpandableListView();

另外,有没有办法从类外部调用 notifyDataSetChanged() 方法。也许您可以从调用 InsertItem 方法的同一个地方调用它。

关于android - 调用 notifyDataSetChanged 时强制 ExpandableListView 更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6374378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com