如何将相同的值分组到 1 个 recyclerview 项目中?

How can I group same values into 1 single recyclerview item?

我有一个 recyclerview,其中我使用 JAVA 列出了来自 Firebase 的费用类别,我希望对类别进行分组,因此我只得到 1 个名为 FOOD、ENTERTAINMENT 等的 recyclerview 项目,以及每个项目的总金额该类别中的项目。

现在这是我的回收站结果;

这是我的适配器 class;

public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ViewHolder> {
    private Context mContext;
    private ArrayList<Expense> categories;

    public CategoriesAdapter(Context mContext, ArrayList<Expense> categories) {
        this.mContext = mContext;
        this.categories = categories;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.categories_retrieve_layout, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Expense expense = categories.get(position);

        holder.category.setText("" + expense.getCategory());
        holder.amount.setText("$ " + formatNumberCurrency(String.valueOf(expense.getAmount())));

    }

    @Override
    public int getItemCount() {
        return categories.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView category;
        private TextView amount;
        private TextView percentage;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            category = itemView.findViewById(R.id.category_name);
            amount = itemView.findViewById(R.id.category_total_amount);
            percentage = itemView.findViewById(R.id.percentage_indicator);

        }
    }

    private static String formatNumberCurrency(String amount) {
        DecimalFormat formatter = new DecimalFormat("###,###,###");
        return formatter.format(Double.parseDouble(amount));
    }
}

这是我读取数据的方式

final Calendar c = Calendar.getInstance();
@SuppressLint("SimpleDateFormat") DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(c.getTime());

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(Objects.requireNonNull(mAuth.getCurrentUser()).getUid());
Query query = databaseReference.orderByChild("date").equalTo(date);
query.addValueEventListener(new ValueEventListener() {
    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        expenses.clear();

        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            Expense expense = dataSnapshot.getValue(Expense.class);
            expenses.add(expense);
        }

        categoriesAdapter.notifyDataSetChanged();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

这里是我期望得到的输出

关于如何为此创建逻辑有什么建议吗?


这是我在尝试使用 Sambhav 时遇到的错误。 K的回答:

您可以在将值添加到列表时检查它是否已经存在。试试这个:

query.addValueEventListener(new ValueEventListener() {
    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        expenses.clear();

        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            Expense expense = dataSnapshot.getValue(Expense.class);
            boolean exists = false;
            int count = 0;
            for(int i = 0; i < expenses.size(); i++){
               count = i;
               exists = expenses.get(i).getCategory().equalsIgnoreCase(expense.getCategory());
               if(exists) break;
            }
            if(exists){
               expenses.get(count).setAmount(expenses.get(i).getAmount()() + expense.getAmount());
            }else{
               expenses.add(expense);
            }
        }

        categoriesAdapter.notifyDataSetChanged();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
       error.printStackTrace();
    }
});