Listview不基于数组适配器上的值更新

时间:2018-08-08 13:44:45

标签: android listview android-arrayadapter

我调用下面的方法来更新我的列表视图

ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
ListAdapter adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);

但是该代码与更改的值相关联,这也是其他代码

private ArrayList<plan_model> getItems_search(String param_cusname) {
    Cursor data = myDb.get_search_plan(pattern_email, param_name);
    int i = 0;
    while (data.moveToNext()) {
        String date = data.getString(3);
        String remarks = data.getString(4);
        items.add(new plan_model(cusname, remarks);
    }
    return items;
}

这是我的分拣机

private ArrayList sortAndAddSections(ArrayList<plan_model> itemList) {

        Collections.sort(itemList);
        plan_model sectionCell;

        tempList.clear();
        tmpHeaderPositions.clear();

        String header = "";
        int addedRow = 0;
        int bgColor = R.color.alt_gray;
        for (int i = 0; i < itemList.size(); i++) {

            String remarks = itemList.get(i).getRemarks();
            String date = itemList.get(i).getDate();

            if (!(header.equals(itemList.get(i).getDate()))) {
                sectionCell = new plan_model(remarks, date);
                sectionCell.setToSectionHeader();
                tmpHeaderPositions.add(i + addedRow);
                addedRow++;
                tempList.add(sectionCell);
                header = itemList.get(i).getDate();
                bgColor = R.color.alt_gray;
            }
            sectionCell = itemList.get(i);
            sectionCell.setBgColor(bgColor);
            tempList.add(sectionCell);
            if (bgColor == R.color.alt_gray) bgColor = R.color.alt_white;
            else bgColor = R.color.alt_gray;
        }

        tmpHeaderPositions.add(tempList.size());
        for (int i = 0; i < tmpHeaderPositions.size() - 1; i++) {
            sectionCell = tempList.get(tmpHeaderPositions.get(i));
            sectionCell.setDate(sectionCell.getDate() + " (" +
                    (tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");
        }

        return tempList;
    }

我的问题是值name发生了变化,但是我的列表视图不是如何更新我的列表视图?因为我需要根据搜索参数进行更新

2 个答案:

答案 0 :(得分:1)

如果您的itemList得到正确更新,则无需创建适配器的另一个实例,只需使用notifyDataSetChanged()

private void createList() {
    ListView list = (ListView) listView.findViewById(R.id.plan_list);
    itemsList = sortAndAddSections(getItems_search(name));
    adapter = new ListAdapter(getActivity(), itemsList);
    list.setAdapter(adapter);
}

private void updateList() {
    sortAndAddSections(getItems_search(name)); // Update itemList without re-assign its value, otherwise the adapter will loose reference
    adapter.notifyDataSetChanged()
}

答案 1 :(得分:0)

getItems_search()的开头添加以下行:

items.clear();

每次名称值更改时,您都必须执行以下操作:

itemsList.clear();
itemsList = sortAndAddSections(getItems_search(name));
list.setAdapter(new ListAdapter(getActivity(), itemsList));