簡體   English   中英

如何使用 Android Studio 簡單地檢索 Firebase 中的所有項目和值

[英]How to Simply retrieve all the items and values in Firebase using Android Studio

這是我的代碼。 我試圖在日志中檢索它,然后在檢索到它時顯示它。 但我認為這似乎不是正確的方法。 希望有人能糾正我,如何輕松檢索 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) {

            }
        });

Kotlin

如果你想從 firebase 雲火庫中讀取數據,你想簡單地編寫這個代碼。

// 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
                    }
                }

暫無
暫無

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

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