实例化和设置新的自定义适配器有效,但notifyOnDataSetChanged()不起作用

时间:2016-05-26 18:41:43

标签: android listview android-adapter

我遇到notifyOnDataSetChanged().问题我可以通过创建新的CustomAdapter来获取列表视图以加载新数据,但不能使用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);
}

虽然以下不起作用 - 但列表视图不会刷新,它只是保持原样,然后才能检索新数据。我已检查是否正确检索了新数据。

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
        ...
    }

我尝试过以下几页的建议无济于事:

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

请试试这个:

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

将数据源对象传递给适配器时,只需按值传递即可。在活动范围中刷新数据时,适配器对此一无所知。这就是Sabeeh在适配器内部创建更新方法的原因,这样他就可以更新其适配器数据源。 - Vinicius Rodrigues Lima

我希望这会对你有所帮助。

答案 1 :(得分:0)

将您的adapter变量声明为ArrayAdapter并尝试使用以下代码 -

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

    adapter.notifyOnDataSetChanged();
}