如何在Android RecyclerView中结合Firestore实现按类别汇总金额
实现Firestore支出数据按类别汇总并展示
1. 创建汇总数据实体类
首先需要一个新的实体类来存储类别-总金额的对应关系,替代原来的Expenses实体用于RecyclerView展示:
public class CategoryTotal { private String category; private double totalAmount; // Firestore序列化需要空构造方法 public CategoryTotal() {} public CategoryTotal(String category, double totalAmount) { this.category = category; this.totalAmount = totalAmount; } // Getter & Setter public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public double getTotalAmount() { return totalAmount; } public void setTotalAmount(double totalAmount) { this.totalAmount = totalAmount; } }
2. 实现数据汇总逻辑
在Firestore查询成功后,遍历拿到的Expenses列表,用HashMap按类别累加金额,再转成CategoryTotal列表:
// 假设你的Firestore查询代码 db.collection("Expenses") .whereEqualTo("email", targetEmail) .whereGreaterThanOrEqualTo("timestamp", startTime) .whereLessThanOrEqualTo("timestamp", endTime) .get() .addOnSuccessListener(querySnapshot -> { List<Expenses> expenseList = querySnapshot.toObjects(Expenses.class); // 按类别汇总金额 HashMap<String, Double> categorySumMap = new HashMap<>(); for (Expenses expense : expenseList) { String category = expense.getCategory(); double amount = expense.getAmount(); // 累加金额,无该类别则默认0 categorySumMap.put(category, categorySumMap.getOrDefault(category, 0.0) + amount); } // 转换为RecyclerView可用的列表 List<CategoryTotal> categoryTotalList = new ArrayList<>(); for (Map.Entry<String, Double> entry : categorySumMap.entrySet()) { categoryTotalList.add(new CategoryTotal(entry.getKey(), entry.getValue())); } // 绑定RecyclerView setupRecyclerView(categoryTotalList); }) .addOnFailureListener(e -> { // 处理查询失败逻辑 });
3. 修改RecyclerView Adapter
替换原Adapter的泛型为CategoryTotal,适配新的实体类:
public class CategoryTotalAdapter extends RecyclerView.Adapter<CategoryTotalAdapter.ViewHolder> { private List<CategoryTotal> mDataList; public CategoryTotalAdapter(List<CategoryTotal> dataList) { this.mDataList = dataList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_category_total, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { CategoryTotal item = mDataList.get(position); holder.tvCategory.setText(item.getCategory()); // 格式化金额为两位小数 holder.tvTotal.setText(String.format("%.2f", item.getTotalAmount())); } @Override public int getItemCount() { return mDataList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView tvCategory, tvTotal; public ViewHolder(@NonNull View itemView) { super(itemView); tvCategory = itemView.findViewById(R.id.tv_category); tvTotal = itemView.findViewById(R.id.tv_total_amount); } } }
4. 调整RecyclerView Item布局
新建item_category_total.xml布局,用于展示类别和汇总后的金额:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:orientation="horizontal" android:justifyContent="space-between"> <TextView android:id="@+id/tv_category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textStyle="bold"/> <TextView android:id="@+id/tv_total_amount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp"/> </LinearLayout>
5. 绑定RecyclerView的辅助方法
添加初始化RecyclerView的方法:
private void setupRecyclerView(List<CategoryTotal> dataList) { RecyclerView recyclerView = findViewById(R.id.your_recyclerview_id); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new CategoryTotalAdapter(dataList)); }
内容的提问来源于stack exchange,提问作者Dulitha Bandaranayake
相关产品推荐
相关产品推荐

