简体   繁体   中英

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

Here is my code. I tried to retrieve it in the log then display it if it is retrieved. But I think it doesn't seem the right way. And hoping someone could correct me, on how to easily retrieve all the items and values in 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

If you want to read data from firebase cloud firestore you want to simple write this code.

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

        }

Although it is not so clear from the question where you want to store the data retrieved from firebase.This may work..Have a try....


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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM