调用notifyDataSetChanged时强制ExpandableListView更新数据 [英] Force ExpandableListView to update data when notifyDataSetChanged is called

查看:148
本文介绍了调用notifyDataSetChanged时强制ExpandableListView更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ExpandableListView和一个ExpandableListAdapter.当我在适配器中调用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 {

构建适配器和列表

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

    }
};

然后有我的适配器

public class ExpandableHomeAdapter extends BaseExpandableListAdapter {

插入方法

public void insertItem(ItemSummary item, int index) {
    items.add(index, item);

    this.notifyDataSetChanged();
}

当我使用多个ArrayAdapter时,使用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?

推荐答案

我认为这是由于您给ExpandableListView充气的方式而发生的.尝试以下操作:代替执行findViewById来获取ELV,而是调用包含ExpandableListView的布局的setContentView.然后致电:

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

此外,还有一种方法可以让您从类外部调用notifyDataSetChanged()方法.也许您可以在调用InsertItem方法的同一位置调用它.

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.

这篇关于调用notifyDataSetChanged时强制ExpandableListView更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆