gpt4 book ai didi

java - 在滚动的可扩展 ListView 中重复并重新定位 'children'

转载 作者:行者123 更新时间:2023-12-02 02:42:59 26 4
gpt4 key购买 nike

我可以将数据添加到可扩展 ListView 中,但是当我的屏幕变满并且我开始滚动时,子数据开始重新定位自己。请检查屏幕截图

enter image description here

在此屏幕截图中,六月的唯一支出是钢包,但当我滚动时,它会更改为DStv Bills,这不应该是这样。 DStv Bills 数据为 7 月数据。

enter image description here

但是当我单击Dstv Bills项目时,弹出对话框会给出Laddle的正确信息。

enter image description here

我的代码如下

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

最佳答案

问题是由于对象重用造成的。该对象已经创建,因此我们应该设置新值。如果您没有设置,它将显示旧值。

您在组 View 中做得正确,但在 subview 中,您做错了。一直设置这个值就可以正常工作了

试试这个,

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

关于java - 在滚动的可扩展 ListView 中重复并重新定位 'children',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45111086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com