Firebase Android计数儿童/徽章 [英] Firebase Android count children/ badge

查看:384
本文介绍了Firebase Android计数儿童/徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试开发一个简单的购物应用程序。将有几个产品类别,我使用不同的活动与ListView的每个类别。当用户选择产品类别(说items1 =饮料) - 新屏幕打开,他可以添加苏打水,可乐...我想添加计数徽章在每个类别显示每个类别的产品数量。
例如对于items,我需要显示5,items1 - 2和items10 / 11 - 显示1:

I try to develop a simple shopping application. There will be a few product categories and I use different activity with ListView for every category. When User choose product category (say items1 ="drinks")- new screen opens and he can add "soda", "cola"... I want to add count badge on every category to show number of products per category. So, for example for "items" I need to display 5, for "items1" - 2 and for "items10/11" - display 1:

我的ActivityItems1代码:

my ActivityItems1 code:

       private Firebase mRef;
       private String mUserId;
       private String itemsUrl;
       private TextView badge;


       itemsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/items1";



    // Set up LisView
    final ListView listView = (ListView) findViewById(R.id.listView);
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,     android.R.layout.simple_list_item_1, android.R.id.text1);
    listView.setAdapter(adapter);  

    // Find badge
         badge =(TextView)findViewById(R.id.badgeView);


    // Use Firebase to populate the list.
    new Firebase(itemsUrl)
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String   s) {
                    adapter.add((String)     dataSnapshot.child("title").getValue());


     Log.e(dataSnapshot.getKey(),    dataSnapshot.getChildrenCount() + "");

     badge.setText(dataSnapshot.getChildrenCount() + "");
                }

运行代码后,我收到了Key和他的孩子的数量:
E-KJGG2driQ6HJI8R7eve:1
E-KJGG3Ua6rmlQYn4IHF:1

After running the code I received Key and number of his children: E-KJGG2driQ6HJI8R7eve: 1 E-KJGG3Ua6rmlQYn4IHF: 1

但是我需要计算密钥/产品ID(类别饮料中的产品),而不是他的孩子,所以每个密钥都有1个子项,因为Key =产品ID和密钥的子项。 ...

it's always 1 child for every Key because Key=Product ID and child of the Key - it's title. But I need to count Keys/ Product IDs (products in category "drinks") and not his children...

推荐答案

使用此数据库,您有两个选项:

With this Database you have two options:

1)使用子项

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
//You must remember to remove the listener when you finish using it, also to keep track of changes you can use the ChildChange
myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Log.e(dataSnapshot.getKey(),dataSnapshot.getChildrenCount() + "");
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

2)使用Value侦听器

2)Use the Value listener

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();

//You can use the single or the value.. depending if you want to keep track
thismyRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snap: dataSnapshot.getChildren()) {
            Log.e(snap.getKey(),snap.getChildrenCount() + "");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

这两种情况的输出

如果您需要保持不断跟踪,最好使用Child事件...因为否则每次

If you need to keep constant tracking is best to use the Child events...because otherwise each time anything changes you will receive the entire json over the network, instead of only the portion of information that was changed

如果你只需要一个快照,你最好使用singleValue,因为这种方式您将同时接收所有信息

If you need a snapshot only once, you better use the singleValue since this way you will receive all the information at the same time

这篇关于Firebase Android计数儿童/徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆