我可以将数据添加到可展开的列表视图中,但是当我的屏幕变满并且开始滚动时,子数据开始重新定位自己。请检查屏幕截图
在此屏幕快照中,六月的唯一支出是Laddle,但是当我滚动查看时,它变为DStv Bills,但事实并非如此。 DStv Bills数据在7月以下。
但是,当我单击“ Dstv Bills”项目时,弹出对话框会显示Laddle的正确信息。
我的代码如下
1)我的ExpandableListAdapter类
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)我的ExpandableListview类中的函数
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));
}
}
最佳答案
问题是由于对象重用。该对象已经创建,因此我们应该设置新值。如果您未设置,它将显示旧值。
您在“组”视图中正确执行,但是在子视图中,您执行错了。一直设置该值,然后它将正常工作
尝试这个,
@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;
}