列表视图未根据阵列适配器上的值进行更新



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

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更改的值,但我的列表视图不是如何更新我的列表视图? 因为我需要根据搜索参数更新它

如果您的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()
}

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

items.clear();

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

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

最新更新