如何使用Android Studio简单地检索Firebase中的所有项和值

如何使用Android Studio简单地检索Firebase中的所有项和值,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,这是我的密码。我试图在日志中检索它,然后在它出现时显示它 已检索。但我认为这似乎不是正确的方式。希望有人能纠正我,关于如何轻松检索firebase中的所有项目和值 final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference() .child("FirstRoot"); databaseReference.add

这是我的密码。我试图在日志中检索它,然后在它出现时显示它 已检索。但我认为这似乎不是正确的方式。希望有人能纠正我,关于如何轻松检索firebase中的所有项目和值

  final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
                .child("FirstRoot");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
//
//                    Map<String,Object> map = (Map<String,Object>) dataSnapshot.getValue();
//                    Object material = map.get("item");
//                    Object value = map.get("value");
//                    int v = Integer.parseInt(String.valueOf(value));
                    String material  = String.valueOf(dataSnapshot.child("item").getValue());
                    Log.d("Item:", material);
//                    String item = dataSnapshot.child("item").getValue().toString();;
////                    Float valuee = Float.parseFloat(dataSnapshot.child("valuee").getValue().toString());
//                    entries.add(new PieEntry(13f, item));
                }
            }

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

            }
        });
final DatabaseReference DatabaseReference=FirebaseDatabase.getInstance().getReference()
.儿童(“第一根”);
databaseReference.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot snapshot){
对于(DataSnapshot DataSnapshot:snapshot.getChildren()){
//
//Map Map=(Map)dataSnapshot.getValue();
//对象材质=map.get(“项目”);
//对象值=map.get(“值”);
//intv=Integer.parseInt(String.valueOf(value));
String material=String.valueOf(dataSnapshot.child(“item”).getValue());
日志d(“项目:”,材料);
//String item=dataSnapshot.child(“item”).getValue().toString();;
////Float valuee=Float.parseFloat(dataSnapshot.child(“valuee”).getValue().toString());
//条目。添加(新条目(13f,条目));
}
}
@凌驾
已取消公共void(@NonNull DatabaseError){
}
});

Kotlin

如果要从firebase cloud firestore读取数据,只需编写以下代码

// Get a Instance from FirebaseFirestore.
val db= FirebaseFirestore.getInstance()
    //Get the user current Id.
    val uId = auth.currentUser!!.uid
    //Instead of XYZ you have to write your collection name and Id in 
    //document.
    db.collection("XYZ").document(uId)
        .get()
        .addOnCompleteListener {
            if (it.isSuccessful){
               //When it is successful you have to do what you want as I have given example that you can understand better through this way you can get userName and you can set into your textView and you can read as many data as you can this is one example.
                val documentSnapShot = it.result
                val firstName = documentSnapShot.getString("first_name")
                tv_Set_ProfileName.text =firstName
                
            }

        }

虽然您想将从firebase检索到的数据存储在何处这个问题还不太清楚。这可能会起作用。试试看


final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
                .child("FirstRoot");
myRef.addValueEventListener(new ValueEventListener() {
                  
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() == null) {
                            Toast.makeText(getApplicationContext(),"Data Not Available",Toast.LENGTH_LONG).show();
                        } else {

                            final String data1 = (Objects.requireNonNull(dataSnapshot.child("data1").getValue())).toString();
                            final String data2 = (Objects.requireNonNull(dataSnapshot.child("data2").getValue())).toString();
                            final String data3 = (Objects.requireNonNull(dataSnapshot.child("data3").getValue())).toString();
                            final String data4 = (Objects.requireNonNull(dataSnapshot.child("data4").getValue())).toString();

                            model_class model = new model_class(data1,data2,data3,data4);
                            //do whatever you want with your data
                             entries.add(new PieEntry(13f, data1));
                          
                        }
                       
                    }
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        throw error.toException(); // never ignore errors
                    }
                }


1) 有关android studio IDE本身的问题,请仅使用
android studio
标记。有关Android编程的一般问题,请使用
Android
标记。2) 当您在调试器中运行此代码并逐行检查时,哪一行不符合您的预期?@FrankvanPuffelen感谢提供有关标记的信息,因为这是我第一次在stackoverflow中询问一些问题。在你的第二季度,这是我朋友的程序,他说当他调试代码时,代码显示为null或值尚未检索。所以你想读取项和值的值,对吗?@AlexMamo是的,就像读取firebase中的项和值并检索它们一样。你还有其他类似的值吗,或者你只想读取这两个值吗?当然,如果你从中学习,请投票。