Click here to Skip to main content
16,004,458 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 xml files

`Header` and `Section`

then i use some condition to segregate those two and the output of that is this


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


It is a listview that groups data with same date and the 1 date header for them.
My question is how can I count each data then update the header? like this

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


Im using `Listview` and `ListAdapter extends ArrayAdapter`

where do i will update?

here

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


or here


Java
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;
        }

Updated

Here is how I transfer the data from database to array

Java
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;
        }


then this where the condition goes

Java
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;
        }



this is where i set it in textview

Java
@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;
            }


What I have tried:

Try googling some codes and apply it but nothing works
Posted
Comments
Richard MacCutchan 3-Aug-18 4:44am    
You need to get the count of the entries before you insert the section header.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900