【问题标题】:Android: Updating a BaseAdapter's ArrayList properly?Android:正确更新 BaseAdapter 的 ArrayList?
【发布时间】:2014-02-18 10:20:22
【问题描述】:

我看过许多示例/教程,这些示例/教程解释了如何更新BaseAdapter 使用的ArrayList。例如:

public class CustomAdapter extends BaseAdapter {
    private ArrayList<Item> listData;
    private LayoutInflater layoutInflater;

    public CustomListAdapter(Context context, ArrayList<Item> listData) {
            this.listData = listData;
            layoutInflater = LayoutInflater.from(context);
    }

    //called to update the ListView
    public void resetList(ArrayList<Item> newList) {
        this.listData = newList;
        this.notifyDataSetChanged();
    }

}

另一种方法是清除并填充listData当前指向的原始ArrayList,并从Activity的线程中调用notifyDataSetChanged()

为什么这是一种经常被推荐的方式?如果我在修改 ArrayList 之后或期间但在调用 notifyDataSetChanged() 之前直接单击 ListView 项目会发生什么?

【问题讨论】:

    标签: java android arraylist listadapter


    【解决方案1】:

    由于您是从 Activity (UI) 线程调用 resetList(),因此您不能在方法执行过程中单击项目,因为单击是由同一个 UI 线程处理的,因此,您只能在之前执行单击或者在方法执行之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多