获取列表计数,然后更新列表视图中的标题

时间:2018-08-02 10:48:07

标签: android listview

我有2个xml文件

HeaderSection

然后我使用某种条件将这两个条件分开,其输出是这样

|8/2/2018|
----------
 Data 1
 Data 2
|8/2/2018|
----------
 Data 1
 Data 2
 Data 3

这是一个列表视图,将具有相同日期和1日期标题的数据分组。 我的问题是如何计算每个数据然后更新标头?像这样

|8/2/2018 (2)|
----------
 Data 1
 Data 2
|8/2/2018 (3)|
----------
 Data 1
 Data 2
 Data 3

我正在使用ListviewListAdapter extends ArrayAdapter

我将在哪里更新?

这里

public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ItemModel cell = (ItemModel) getItem(position);

   if (cell.isSectionHeader()) {
        //Display Date in Header XML
   } else {
        //Display in Section XML
   }
}

或这里

private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {

        ArrayList<ItemModel> tempList = new ArrayList<>();
        Collections.sort(itemList);

        String header = "";
        for (int i = 0; i < itemList.size(); i++) {
            if (!(header.equals(itemList.get(i).getDate()))) {
                String data = itemList.get(i).getRemarks();
                String date = itemList.get(i).getDate()
                ItemModel sectionCell = new ItemModel(date,data);
                sectionCell.setToSectionHeader();
                tempList.add(sectionCell);
                header = itemList.get(i).getDate();
            }

            tempList.add(itemList.get(i));
        }
        return tempList;
    }

已更新

这是我将数据从数据库传输到数组的方式

private ArrayList<ItemModel> getItems() {
        Cursor data = myDb.get_plan(email);
        ArrayList<ItemModel> items = new ArrayList<>();
        while (data.moveToNext()) {
            String date = data.getString(3);
            String data1 = data.getString(4);
            items.add(new ItemModel(date,data1));
        }
        return items;
    }

然后这就是条件

private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {

        ArrayList<ItemModel> tempList = new ArrayList<>();

        String header = "";
        for (int i = 0; i < itemList.size(); i++) {
            if (!(header.equals(itemList.get(i).getDate()))) {
                String date = itemList.get(i).getDate();
                String getData1 = itemList.get(i).getData1();
                ItemModel sectionCell = new ItemModel(date,data1);
                sectionCell.setToSectionHeader();
                tempList.add(sectionCell);
                header = itemList.get(i).getDate();
            }
            tempList.add(itemList.get(i));
        }
        return tempList;
    }

这是我在textview中设置的位置

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ItemModel cell = (ItemModel) getItem(position);

            if (cell.isSectionHeader()) {
                v = inflater.inflate(R.layout.section_header, null);
                v.setClickable(false);
                TextView header = (TextView) v.findViewById(R.id.list_date);
                header.setText(cell.getDate());
            } else {
                v = inflater.inflate(R.layout.listview_plan, null);
                TextView tv_data1 = v.findViewById(R.id.list_data1);
                tv_cusname.setText(cell.getData1());
            }

            return v;
        }

2 个答案:

答案 0 :(得分:0)

稍后可以在获取数据列表时使用setText,然后在arraylist.length()的帮助下将文本设置为部分。

答案 1 :(得分:0)

尝试一下:

    private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {

    ArrayList<ItemModel> tempList = new ArrayList<>();
    ArrayList<Integer> tmpHeaderPositions = new ArrayList<>(); // Added

    String header = "";

    ItemModel sectionCell; // Changed
    int addedRow = 0; // Edited 
    for (int i = 0; i < itemList.size(); i++) {
        if (!(header.equals(itemList.get(i).getDate()))) {
            String date = itemList.get(i).getDate();
            String getData1 = itemList.get(i).getData1();
            sectionCell = new ItemModel(date,data1); // Changed
            sectionCell.setToSectionHeader();
            tmpHeaderPositions.add(i + addedRow); // Edited
            addedRow++; // Edited
            tempList.add(sectionCell);
            header = itemList.get(i).getDate();
        }
        tempList.add(itemList.get(i));
    }

    // Added this code block
    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;
}

希望有帮助!