簡體   English   中英

如何從 firebase 結構而不是 Boolean 值中檢索字段?

[英]How can i retrieve the fields from the firebase structure instead of the Boolean values?

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

這是代碼

 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) {

                            }
                        });

這條線是罪魁禍首

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

你應該試試這個

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

當只想讀取實時數據庫中的某些鍵時,無需創建任何 class。 僅當您要將 map 節點轉換為特定 class 的 object 時才需要類。 這意味着 class 中的字段應該與數據庫中的字段完全相同。 所以這不是你的情況,因為這些 UID 是動態添加的。 要讀取這些鍵,請使用以下代碼行:

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!
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM