调用notifyDataSetChanged时强制ExpandableListView更新数据

发布于 2024-11-15 21:54:27 字数 1617 浏览 7 评论 0原文

我有一个 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 ArrayAdapters. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2024-11-22 21:54:27

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

 ExpandableListView homeList = getExpandableListView();

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文