I have a ListView
. Within the list view, I want to fetch data from the Firebase database. I am able to show title, count in the activity. I want to fetch how many status shows unread and it will be semester Wise .
unread_count not working.
Below is the database structure:
I have tried below but unable to fetch unread_count . It always shows 0
databaseReference = FirebaseDatabase.getInstance().getReference("All_Image_Uploads_Database");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<ListType>();
list.clear();
for (DataSnapshot postSnapshot:dataSnapshot.getChildren()){
String count = String.valueOf(postSnapshot.getChildrenCount());
String title = postSnapshot.getKey();
String unread_count = String.valueOf(postSnapshot.child("status").getChildrenCount());
ListType listType = new ListType(title, count, unread_count);
list.add(listType);
}
ListTypeList listTypeList = new ListTypeList(HomeActivity.this, list);
listView.setAdapter(listTypeList);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I need to fetch how many "status" shows "unread". It means to count the numbers of "unread" in semester wise.
I need your suggestion of how I will get the required data.
According to this comment:
"Semester I": unread> 1, , "Semester II": unread> 3, "Semester III":unread> 2 likewise I need result. I want to get the result from all Semester
And this:
Semester is dynamic. How will I fetch the result, Plz suggest Sir
I can tell that you cannot count that in a single query. You have to use either a query for each semester separately and count them one by one or you can duplicate data. The best solution I can think of might be to change your database schema. Your new schema might look like this:
Firebase-root
|
--- allSemesters
|
--- -M7_k ... 8Agw
| |
| --- semester: 1
| |
| --- //Other data
|
--- -M7a1 ... zZoO
|
--- semester: 2
|
--- //Other data
Using a schema like this, you can solve two problems. One would be to get the count of all objects, and the second would be get the count of a particular semester. As you requested in your question, to count all those objects, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference allSemestersRef = rootRef.child("allSemesters");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("TAG", "Count: " + dataSnapshot.getChildrenCount());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
allSemestersRef.addListenerForSingleValueEvent(valueEventListener);
You can also take a look at my answer from the following post:
If you only want to get the object of a particular semester, you should use the following query:
allSemestersRef.orderByChild("semerster").equalTo(2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.