Skip to content Skip to sidebar Skip to footer

Retrieve Data From Firebase Database On Button Click

I'm writing an app that allows users to collaborate on inventory by uploading data to a database hosted by firebase. I can write data to it no problem, however it only wants to let

Solution 1:

You need to use addListenerForSingleValueEvent() to retrieve data

DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("<childName>").addListenerForSingleValueEvent(newValueEventListener(){
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {

        }

        @OverridepublicvoidonCancelled(DatabaseError databaseError) {

        }
});

Here in child you need to mention your child node name

If you want to get change of whole database, simply remove child so it look like mDatabase.addListenerForSingleValueEvent()

Solution 2:

You can use addValueEventListener() to retrieve data when an event is triggered.

In your onClick() method you can write :

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

// In the database whenever any data is changed then the below code snipped is going to be executed.
mDatabase.child("<dbLocationTag>").addValueEventListener(newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {

                // the changed value is going to be stored into the dataSnapshot// to retrieve value from dataSnapshot we writeString value = dataSnapshot.getValue(String.class);
                mValueView.setText(value);

                // NOTE: if you've multiple childs of same node then you can use Map to display all of them : EG: /*Map<String, String> map = dataSnapshot.getValue(Map.class);
                String val1 = map.get("val1");
                String val2 = map.get("val2");
                String val3 = map.get("val3");
                Log.v("TAG", "Value 1 "+val1);
                Log.v("TAG", "Value 2 "+val2);
                Log.v("TAG", "Value 3 "+val3);
*/
            }

            @OverridepublicvoidonCancelled(FirebaseError firebaseError) {

                mValueView.setText(firebaseError.toString());

            }
        });

Solution 3:

addButton.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        DatabaseReference mRef = mDatabaseReference.getDatabase().getReference("users");
        ValueEventListener valueEventListener = newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    Log.d(TAG, "" + ds.getKey());
                }
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        };
    }
});
mRef.addListenerForSingleValueEvent(valueEventListener);

The above method worked for me. Hope it helps for you, too.

Post a Comment for "Retrieve Data From Firebase Database On Button Click"