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:
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() + "");
}
After running the code I received Key and number of his children: E-KJGG2driQ6HJI8R7eve: 1 E-KJGG3Ua6rmlQYn4IHF: 1
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...