【问题标题】:Force ExpandableListView to update data when notifyDataSetChanged is called调用 notifyDataSetChanged 时强制 ExpandableListView 更新数据
【发布时间】:2011-06-16 15:27:05
【问题描述】:

我有一个 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();
}

当我使用多个ArrayAdapters 时,使用notifyDataSetChanged() 的相同方法也有效。然而,现在,虽然一切正常并且数据实际上已更改,但在我(手动)折叠然后展开组之前,屏幕不会使用新数据更新。我没有尝试以编程方式折叠/展开,但我想将其保存为最后的手段。如何在数据更改时更新视图?

【问题讨论】:

    标签: android view expandablelistview expandablelistadapter


    【解决方案1】:

    我认为这是因为你给ExpandableListView 充气的方式。试试这个:不要使用findViewById 来获取ELV,而是调用包含ExpandableListView 的布局的setContentView。然后调用:

     ExpandableListView homeList = getExpandableListView();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-04
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2011-02-24
      相关资源
      最近更新 更多