You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Firebase购物车实现:如何兼顾价格同步与数据加载效率?

安卓平台购物车功能Firebase实现方案探讨

当前设计思路

数据库节点结构

Items:
   |
   |--item1Key--
               |
               |--price:10
   |
   |--itemkey2--
               |
               |--price:20

User:
|
|--inCartItems:
          |
          |--itemKey1
          |
          |--itemKey2

当前仅将加入购物车的商品Key存储到inCartItems节点,而非价格信息。这么做的原因是:如果管理员修改Items节点中的商品价格,若inCartItems节点存储价格则无法同步更新,导致购物车显示价格与实际商品价格不一致。

请问是否有更优的实现方案?欢迎各位提出建议。

现存问题

仅存储商品Key的方式会引发实际开发中的问题,以下是收藏功能中采用相同实现方式的代码示例(购物车逻辑一致):

// Here I successfully got all the keys in lyKey
for(ItemsExploreModel favkeys : ltKey)
{
    // Toast.makeText(getContext(), favkeys.getKey(), Toast.LENGTH_SHORT).show();
    mDatabase.getReference().child(FirebaseVar.ALLITEMS).child(favkeys.getKey()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot2) {
            if (snapshot2.exists())
            {
                ItemsExploreModel adp = snapshot2.getValue(ItemsExploreModel.class);
                ItemsExploreModel adp2 = new ItemsExploreModel(snapshot2.getKey());

                list.add(adp);
                listKey.add(adp2);
            }

        }

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

        }
    });
}

内容的提问来源于stack exchange,提问作者GajjarSVNIT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 09:35:16