rsyed0
rsyed0

Reputation: 175

Android - Value Added Listener causes app to crash

I am trying to read a list of users from a DatabaseReference, but the code causes the app to crash.

firebase.child(subject).addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot snapshot) {
       Map<String,String> td = (HashMap<String,String>) snapshot.getValue();
       users = (List)td.keySet();
   }
   @Override
   public void onCancelled(DatabaseError firebaseError) {
       Toast.makeText(context,"Internet connection failed",Toast.LENGTH_LONG).show();
   }
});

Upvotes: 1

Views: 634

Answers (1)

Infinite Loops
Infinite Loops

Reputation: 681

The correct way to call the data is

databaseReference.addValueEventListener (new ValueEventListener)

Inside onDataChange,

String string = dataSnapshot.getValue(String.class);
textView.setText(string);

That .child() is declared above outside your onCreate,

DatabaseReference db = Master.getInstance().getReference();
DatabaseReference text1 = db.child("databseValueNameYouGiveInConcolse");

Upvotes: 1

Related Questions