Firebase Android 计数 children/ 徽章
Firebase Android count children/ badge
我尝试开发一个简单的购物应用程序。会有一些产品类别,我对每个类别使用不同的 activity 和 ListView。当用户选择产品类别时(比如 items1 ="drinks")- 新屏幕打开,他可以添加 "soda"、"cola"... 我想在每个类别上添加计数徽章以显示数量每个类别的产品。
因此,例如 "items" 我需要显示 5,"items1" - 2 和 "items10/11" - 显示 1:
我的 ActivityItems1 代码:
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() + "");
}
在 运行 代码之后我收到了他的 children 的密钥和号码:
E-KJGG2driQ6HJI8R7eve: 1
E-KJGG3Ua6rmlQYn4IHF: 1
每个密钥总是 1 child,因为密钥=产品 ID 和密钥的 child - 它是标题。 但我需要计算密钥/产品 ID(类别 "drinks" 中的产品)而不是他的 children...
我不确定,但它可以用于解析。尝试
badgeView.setText(nums+"");
也许有帮助。
有了这个数据库,您有两个选择:
1)使用Child添加
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,而不仅仅是已更改的部分信息
如果你只需要一次快照,你最好使用singleValue,因为这样你会同时收到所有信息
我尝试开发一个简单的购物应用程序。会有一些产品类别,我对每个类别使用不同的 activity 和 ListView。当用户选择产品类别时(比如 items1 ="drinks")- 新屏幕打开,他可以添加 "soda"、"cola"... 我想在每个类别上添加计数徽章以显示数量每个类别的产品。 因此,例如 "items" 我需要显示 5,"items1" - 2 和 "items10/11" - 显示 1:
我的 ActivityItems1 代码:
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() + "");
}
在 运行 代码之后我收到了他的 children 的密钥和号码: E-KJGG2driQ6HJI8R7eve: 1 E-KJGG3Ua6rmlQYn4IHF: 1
每个密钥总是 1 child,因为密钥=产品 ID 和密钥的 child - 它是标题。 但我需要计算密钥/产品 ID(类别 "drinks" 中的产品)而不是他的 children...
我不确定,但它可以用于解析。尝试
badgeView.setText(nums+"");
也许有帮助。
有了这个数据库,您有两个选择:
1)使用Child添加
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,而不仅仅是已更改的部分信息
如果你只需要一次快照,你最好使用singleValue,因为这样你会同时收到所有信息