Home > Back-end >  How can i retrieve the fields from the firebase structure instead of the Boolean values?
How can i retrieve the fields from the firebase structure instead of the Boolean values?

Time:09-26

on this link is a screenshot of my Firebase Realtime Database structure, instead of getting the Boolean values I want to get the fields that are holding the Boolean values, I have created a model for that to get the data from the database structure,

this is the error am getting com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Boolean to type com.stys.kneckenyapastpapers.model.course_followers

here's the code

 mDatabase = FirebaseDatabase.getInstance().getReference("Followers").child("Course").child("AGRI");
                        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {

                                for (DataSnapshot di : snapshot.getChildren()) {
                                    course_followers course_followers = di.getValue(course_followers.class);
                                    follower.add(course_followers);
                                    if (dataSnapshot.child(user.getId()).exists()) {
                                        mUSer.add(user);
                                    }

                                }
                                }
                                Toast.makeText(getContext(), String.valueOf(follower.size()), Toast.LENGTH_SHORT).show();


                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });

CodePudding user response:

This line is culprit

course_followers course_followers = di.getValue(course_followers.class);

You should try this instead

Boolean course_followers = Boolean.parseBoolean(di.getValue().toString());

CodePudding user response:

There is no need to create any class when only want to read only some keys in the Realtime Database. Classes are required only when you want to map a node into an object of a particular class. This means that the fields inside the class should be exactly the same as the ones in the database. So it's not your case because those UIDs are dynamically added. To read those keys, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference agriRef = db.child("Followers").child("Course").child("AGRI");
agriRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String uid = ds.getKey();
                mUSer.add(uid);
                Log.d("TAG", uid);
            }
            Toast.makeText(getContext(), String.valueOf(mUSer.size()), Toast.LENGTH_SHORT).show();
            //It should toast the size of mUSer list.
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});
  • Related