问题描述
我尝试开发一个简单的购物应用程序.将有几个产品类别,我对每个类别使用不同的 ListView 活动.当用户选择产品类别时(比如 items1 ="drinks")- 新屏幕打开,他可以添加苏打水"、可乐"......我想在每个类别上添加计数徽章以显示每个类别的产品数量.因此,例如,对于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() + "");
}
运行代码后,我收到了密钥和他孩子的数量:E-KJGG2driQ6HJI8R7eve: 1E-KJGG3Ua6rmlQYn4IHF:1
After running the code I received Key and number of his children:E-KJGG2driQ6HJI8R7eve: 1E-KJGG3Ua6rmlQYn4IHF: 1
每个密钥总是有 1 个子项,因为密钥 = 产品 ID 和密钥的子项 - 它是标题.但我需要计算密钥/产品 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)使用值监听器
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 事件...因为否则每次发生任何更改时,您都会通过网络收到整个 json,而不仅仅是更改的部分信息
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 计数儿童/徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!