Firebase Data retrieval to string

31 views Asked by At

I have been trying to get data into string for almost 5 hours now. I have referred the previous questions related to the same and tried doing it but not able to store the data into a string.

This is the code snippet I am working on:

@Override
public void Click(String name) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("Club_Screen").child(name);
    Log.d("Name", name);

    mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String insta = snapshot.child("instagramLink").getValue().toString();
                String github = snapshot.child("github").getValue().toString();

                if (insta != null) {
                    Log.d("Insta", insta);
                } else {
                    Log.d("Insta", "Instagram link is null");
                }

                if (github != null) {
                    Log.d("Github", github);
                } else {
                    Log.d("Github", "GitHub link is null");
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
           Log.d("Error",databaseError.getMessage());

        }
    });
}

This is the Log Message I am getting.

This is the realtime database for my app

I am expecting to store the links from database to strings so that I can redirect the app to that particular link.

Answer To the Question

@Override
public void Click1(String name) {
    DatabaseReference root = FirebaseDatabase.getInstance().getReference();
    DatabaseReference reference = root.child("Club_Screen");
    com.google.firebase.database.Query query = reference.orderByChild("name").equalTo(name);

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snap: dataSnapshot.getChildren()) {
                String github = (String) snap.child("github").getValue();
                gotoUrl(github);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
0

There are 0 answers