Reputation: 741
I am able to add data into my expandable listview, but when my screen gets full, and i start scrolling, the children data starts to re-position themselves.Kindly check the screenshots
In this screenshot the only expense in June is Laddle, but when i scroll it changes to DStv Bills which is not supposed to be so. The DStv Bills data is UNDER July.
But when i click the Dstv Bills item, the pop-up dialog gives the right informationof Laddle.
My code is as follows
1) My ExpandableListAdapter Class
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ParentRow> parentRowArrayList;
private ArrayList<ParentRow> originalList;
public MyExpandableListAdapter(Context context, ArrayList<ParentRow> originalList) {
this.context = context;
this.parentRowArrayList= new ArrayList<>();
this.parentRowArrayList.addAll(originalList);
this.originalList = new ArrayList<>();
this.originalList.addAll(originalList);
}
@Override
public int getGroupCount() {
return parentRowArrayList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return parentRowArrayList.get(groupPosition).getChildList().size();
}
@Override
public Object getGroup(int groupPosition ) {
return parentRowArrayList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return parentRowArrayList.get(groupPosition).getChildList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition ;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
ParentRow parentRow = (ParentRow) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_parent_list_group, null);
}
TextView parentHeaderText = (TextView) convertView
.findViewById(R.id.parentHeaderText);
TextView parentHeaderAmount= (TextView) convertView
.findViewById(R.id.parentHeaderAmount);
parentHeaderText.setText(parentRow.getName().trim());
parentHeaderAmount.setText(parentRow.getAmount().trim());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
ChildRow childRow = (ChildRow) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_list_item, null);
ImageView childIcon = (ImageView) convertView.findViewById(R.id.ivExp);
childIcon.setImageResource(childRow.getIcon());
final TextView childTitleText = (TextView) convertView
.findViewById(R.id.childTitleText);
final TextView childAmountText = (TextView) convertView
.findViewById(R.id.childAmountText);
childTitleText.setText(childRow.getTitle().trim());
childAmountText.setText(childRow.getAmount()+"");
// final View finalConvertView =convertView;
}
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
public void filterData(String query){
query=query.toLowerCase();
parentRowArrayList.clear();
if(query.isEmpty()){
parentRowArrayList.addAll(originalList);
}else{
for(ParentRow parentRow:originalList){
ArrayList<ChildRow> childList = parentRow.getChildList();
ArrayList<ChildRow> newList = new ArrayList<>();
for(ChildRow childRow:childList){
if(childRow.getTitle().toLowerCase().contains(query)){
// Toast.makeText(context, childRow.getTitle().toString(), Toast.LENGTH_SHORT).show();
newList.add(childRow);
}
}//end for ChildRow childRow:childList
if(newList.size()>0){
ParentRow newParentRow = new ParentRow(parentRow.getName(),parentRow.getAmount(),newList);
parentRowArrayList.add(newParentRow);
}
} // end for ParentRow parentRow:originalList
} //end else
notifyDataSetChanged();
}
public ArrayList<ParentRow> getparentRowArrayList() {
return parentRowArrayList;
}
}
public void filterData(String query){
query = query.toLowerCase();
Log.v("MyListAdapter", String.valueOf(_listDataHeader.size()));
_listDataHeader.clear();
if(query.isEmpty()){
_listDataHeader.addAll(originalList);
}
else {
for(String continent: originalList){
String countryList = continent;
ArrayList<String> newList = new ArrayList<>();
if(countryList.toLowerCase().contains(query) ||
countryList.toLowerCase().contains(query)){
newList.add(countryList);
}
if(newList.size() > 0){
// Continent nContinent = new Continent(continent.getName(),newList);
_listDataHeader.add(String.valueOf(newList));
}
}
}
Log.v("MyListAdapter", String.valueOf(_listDataHeader.size()));
notifyDataSetChanged();
}
}
2) My functions in my ExpandableListview Class
private void displayList(){
loadData();
listAdapter = new MyExpandableListAdapter(MyExpandableListViewClass.this, parentRowArrayList);
myList.setAdapter(listAdapter);
}
private void loadData() {
//listDataHeader = new ArrayList<String>();
int monthVal=0;
for (monthVal = 1; monthVal <= 12; monthVal++) {
ArrayList <ChildRow> childRows = new ArrayList<>();
ParentRow parentRow ;
String listDataHeaderName="Default";
switch (monthVal) {
case 1:
listDataHeaderName = "January";
// listDataHeader.add(listDataHeaderName);
break;
case 2:
listDataHeaderName = "February";
//listDataHeader.add(listDataHeaderName);
break;
case 3:
listDataHeaderName = "March";
// listDataHeader.add(listDataHeaderName);
break;
case 4:
listDataHeaderName = "April";
// listDataHeader.add(listDataHeaderName);
break;
case 5:
listDataHeaderName = "May";
//listDataHeader.add(listDataHeaderName);
break;
case 6:
listDataHeaderName = "June";
// listDataHeader.add(listDataHeaderName);
break;
case 7:
listDataHeaderName = "July";
// listDataHeader.add(listDataHeaderName);
break;
case 8:
listDataHeaderName = "August";
// listDataHeader.add(listDataHeaderName);
break;
case 9:
listDataHeaderName = "September";
//listDataHeader.add(listDataHeaderName);
break;
case 10:
listDataHeaderName = "October";
//listDataHeader.add(listDataHeaderName);
break;
case 11:
listDataHeaderName = "November";
// listDataHeader.add(listDataHeaderName);
break;
case 12:
listDataHeaderName = "December";
// listDataHeader.add(listDataHeaderName);
break;
}
//Recordingexpenditure for previous years
//Formatting Amounts
DecimalFormat formatter = new DecimalFormat("#,###.00");
int counter = 0;
int mYear=0;
String fin_amount=null;
String date="herh";
String amount=null;
String title, category="uu";
usersManager= new UsersManager(this);
id = usersManager.getUserId(getUserEmail());
expenseManager.sendID(id);
allExpenses = expenseManager.getMonthExpenses(monthVal);
for (Expense expense : allExpenses) {
allTitles.add(expense.getTitle());
counter++;
}
//Get date and year
if (allExpenses.isEmpty()) {
continue;
}
// else if(mYear!=current_year){
// Toast.makeText(this, "Not in 2017", Toast.LENGTH_SHORT).show();
// }
else if (!allExpenses.isEmpty()){
//Toast.makeText(this, " IN " +listDataHeaderName, Toast.LENGTH_SHORT).show();
// Getting all the titles
for (int i = 0; i < counter; i++) {
title = allTitles.get(i);
expense = expenseManager.getExpenseExcel(title);
// date= expense.getDate();
// String str[] = date.split("-");
// mYear = Integer.parseInt(str[0]);
// int mMonth=Integer.parseInt(str[1])-1;
category = expense.getCategory();
amount =formatter.format(expense.getAmount());
title = expense.getTitle();
//Child Data
getChildData(title,category,amount, childRows);
///Toast.makeText(this, expense.getDate().toString() +"-" +i, Toast.LENGTH_SHORT).show();
//Get the original amount, format it, and append CUrrency
double header_amount=overviewManager.getTotalExpenditurePerMonth_Budget
(monthVal);
//Toast.makeText(this, "" + header_amount, Toast.LENGTH_SHORT).show();
fin_amount = (currencyManager.getCurrency(MyExpandableListViewClass.this) + " " + header_amount);
}
parentRow = new ParentRow(listDataHeaderName,fin_amount,childRows);
parentRowArrayList.add(parentRow);
allTitles.clear();
allExpenses.clear();
}
}
}
private void getChildData(String title, String category, String child_amount, ArrayList<ChildRow> childRows) {
String fin_amount = (currencyManager.getCurrency(MyExpandableListViewClass.this) + " " + child_amount);
switch (category){
case "Food and Drinks":
childRows.add(new ChildRow(R.drawable.food, fin_amount,title));
break;
case "Transportation":
childRows.add(new ChildRow(R.drawable.transpo,fin_amount, title));
break;
case "Health":
childRows.add(new ChildRow(R.drawable.health,fin_amount, title));
break;
case "Housing":
childRows.add(new ChildRow(R.drawable.house, fin_amount,title));
break;
case "Education":
childRows.add(new ChildRow(R.drawable.education, fin_amount,title));
break;
case "Technical":
childRows.add(new ChildRow(R.drawable.technical,fin_amount, title));
break;
case "Clothing":
childRows.add(new ChildRow(R.drawable.clothing,fin_amount, title));
break;
case "Utilities":
childRows.add(new ChildRow(R.drawable.utilities,fin_amount, title));
break;
case "Other":
childRows.add(new ChildRow(R.drawable.etc,fin_amount, title));
break;
default:
childRows.add(new ChildRow(R.drawable.etc,fin_amount, title));
}
}
Upvotes: 0
Views: 591
Reputation: 11632
The problem is because of object reuse. the object is already created so we should set the new value. if you didn't set it will show the old values.
You did correctly in Group view, but in child view, you did wrong. Set the value all the time, then it will work fine
Try this,
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
ChildRow childRow = (ChildRow) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_list_item, null);
}
ImageView childIcon = (ImageView) convertView.findViewById(R.id.ivExp);
childIcon.setImageResource(childRow.getIcon());
final TextView childTitleText = (TextView) convertView
.findViewById(R.id.childTitleText);
final TextView childAmountText = (TextView) convertView
.findViewById(R.id.childAmountText);
childTitleText.setText(childRow.getTitle().trim());
childAmountText.setText(childRow.getAmount()+"");
// final View finalConvertView =convertView;
return convertView;
}
Upvotes: 2