在滚动的可扩展listview中复制和重新定位“子对象”

gdx19jrr  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(361)

我可以将数据添加到我的可扩展列表视图中,但是当我的屏幕满了,我开始滚动时,子数据开始重新定位。请检查屏幕截图

在这个截图中,6月份唯一的开销是laddle,但是当我滚动它时,它会变成dstv账单,这是不应该的。dstv账单数据在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));
            }
        }
sd2nnvve

sd2nnvve1#

问题在于对象重用。对象已经创建,所以我们应该设置新值。如果不设置,它将显示旧值。
您在“组”视图中的操作是正确的,但在“子”视图中的操作是错误的。一直设置这个值,它就可以正常工作了
试试这个,

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

相关问题