如果多行符合特定条件,则将它们合并到 ListView 中?

Merging multiple rows together in a ListView if they match a certain criteria?

我有一个 ListView 可以跟踪支付的金额,如果用户使用相同类型的支付方式进行两次支付,那么列表中的这两个条目应该合并为一个并添加金额。

Payment 1 -   - Check
Payment 2 - 0 - Check

以上在 ListView 中看起来是这样的:

0 - Check
 - Check

因此确实添加了金额,但我删除持有 50 美元金额的行的逻辑不起作用...不太清楚为什么,但重点是将它们合并在一起并删除持有 50 美元的不必要的行。

这是我的更新方法的相关代码和扩展 ArrayAdapter<ClassObject>.

的适配器 class 的 getView 方法

getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.fragment_payscreen_tab2_listgrid_row, null);
    }

    //Get item from list 
    InvoiceListModel item = this.getItem(position);
    if (item != null) {
        TextView txtAmount        = (TextView)v.findViewById(R.id.PayScreen_Tab2_Columns_txtAmount);
        TextView txtPaidBy        = (TextView)v.findViewById(R.id.PayScreen_Tab2_Columns_txtPaidBy);

        txtPaidBy.setText(item.paidBy);
        txtAmount.setText(item.amount);
    }

    return v;
}

适配器自定义更新方式

 public boolean update(InvoiceListModel object){
        for(int x=0; x< uiListViewCollectionData.size(); x++){
            InvoiceListModel tempObj = uiListViewCollectionData.get(x);
            if(tempObj != null &&
               tempObj.paidBy.equals(object.paidBy)){

                //Set the data on the temp object
                tempObj.amount  = uiListViewCollectionData.get(x).amount.plus(object.amount);

                //Remove the old outdated object from datasource
                uiListViewCollectionData.remove(x);
                this.notifyDataSetChanged();

                //Add the new model containing the new amount back to list
                uiListViewCollectionData.add(tempObj);
                this.notifyDataSetChanged();

                return true;
            }
        }
        return false;
    }

我很确定 notifyDataSetChanged()async,这导致下一行快速执行。是否可以在我需要时立即直接渲染 ListView UI 组件?

如何将数据传递给列表?

您应该 'repack' 您的 old_data 并将 new_data 传递给适配器。类似于:

new_data = repack (old_data);
myAdapter = new MyAdapter(new_data);

因此,如果 old_data 有 50 个项目,新数据将只有 35 个(例如),而您在列表中显示这 35 个项目。合并逻辑应该在 getView() 之外。