• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Get list count then update header in listview

I have 2 xml files

`Header` and `Section`

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


|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

|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

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


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

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

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

@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;
}
 
Back
Top Bottom