• 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

Code:
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


Code:
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

Code:
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

Code:
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

Code:
[USER=1021285]@override[/USER]
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;
}
 
Last edited by a moderator:
Code tags added to make the code slightly more readable.
 
This question will get a better response if you

a) Format the code correctly, with proper indentation
b) Include the whole class(es), rather than disjointed code fragments that don't indicate how they relate to each other.
 
Back
Top Bottom