android - 实例化和设置新的自定义适配器有效,但 notifyOnDataSetChanged() 无效

标签 android listview android-adapter

我在使用 notifyOnDataSetChanged() 时遇到问题。 我可以通过创建新的 CustomAdapter 来让我的 ListView 加载新数据,但不能使用 notifyOnDataSetChanged()。

private void repopulateListView(JSONObject resp) throws JSONException {
    arrayList.clear();
    List<MyObject> newArrayList = getData(); 
    arrayList.addAll(newArrayList); //I've checked that arrayList contains new data

    /* WORKS, new data is loaded in the list view */
    adapter = new CustomAdapter(MainActivity.this, arrayList);
    listView.setAdapter(adapter);
}

而以下内容不起作用 - ListView 未刷新,它只是保持检索新数据之前的状态。我已检查新数据是否已正确检索。

private void repopulateListVIew(JSONObject resp) throws JSONException {
    arrayList.clear();
    List<MyObject> newArrayList = getData(); 
    arrayList.addAll(newArrayList); //I've checked that arrayList contains new data

    /* DOES NOT WORK, list view does not refresh with new data */
    ((BaseAdapter)adapter).notifyOnDataSetChanged();
}

我的适配器是这样定义的:

adapter = new CustomAdapter(MainActivity.this, arrayList);
listView = (ListView) findViewById(R.id.list_view); 
listView.setAdapter(adapter);
listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //additional code
                    startActivity(DetailsActivity);
                }
            }
    );

更新:@Sabeeh 的回答有效。根据下面@Sabeeh 和@ViniciusRodriguesLima 的回答,我不得不按如下方式编辑我的 CustomAdapter.java 以引用 list 变量(除了添加 update() 方法):

public class CustomAdapter extends ArrayAdapter<MyObject> {
    private Context context;
    private List<MyObject> list = new ArrayList<>(); //add this variable

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inf = LayoutInflater.from(getContext());
        View customView = inf.inflate(R.layout.custom_beer_row, parent, false);
        //final MyObject chosenObj = getItem(position); Changed to the statement below
        final MyObject chosenObj = list.get(position); //this is CORRECT
        ...
    }

我尝试了以下页面中的建议但无济于事:

如有任何帮助,我们将不胜感激。

最佳答案

请试试这个:

private void repopulateListVIew(JSONObject resp) throws JSONException {
    arrayList.clear();
    List<MyObject> newArrayList = getData();
    arrayList.addAll(newArrayList);
    adapter.update(arrayList);
}

并在您的 CustomAdapter 类中编写以下函数

public void update(ArrayList<MyObject> list) {
    //replace arrayList variable with your class ArrayList variable
    this.arrayList = list;
    this.notifyDataSetChanged();
}

当您将数据源对象传递给适配器时,您只是按值传递它。当您在 Activity 范围内刷新数据时,您的适配器对此一无所知。这就是为什么 Sabeeh 在适配器内部创建了一个更新方法,这样他就可以更新其适配器数据源。 – Vinicius Rodrigues Lima

希望对你有帮助

关于android - 实例化和设置新的自定义适配器有效,但 notifyOnDataSetChanged() 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468715/

相关文章:

java - 对话框中的取消按钮不关闭对话框

java - 在异步任务中使用外部类变量

java - 无法从 ApplicationAdapter 调用 Activity

android - RecyclerView 适配器在滚动期间采用错误的值

java - 从主 Activity 调用方法到 fragment 适配器

flutter - Android 上的 Diffie Hellman 和 AES

android - 在android中阻止来电

listview - 如何使用按钮从 VB6 ListView 中删除行(项目)?

android - CursorIndexOutOfBoundsException 错误

android - 单击 ListView 上的监听器