Noah
Noah

Reputation: 41

Firebase: Accessing ArrayList Inside User Obeject

I'm attempting to access the values that are stored in an arraylist under a user item called contacts. My Current code is located below but it throws an exception of failing to convert a hash map to string on the indicated line. If anyone has any tips on how to solve this or a better solution I would appreciate it.

mDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            GenericTypeIndicator<List<String>> t = new 
     GenericTypeIndicator<List<String>>() {};
            -> List<String> yourStringArray = dataSnapshot.getValue(t);
            Log.d("demo", yourStringArray.toString());
        }

enter image description here

Upvotes: 0

Views: 32

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

To access the attributes under contacts:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("users").child(userid).child("contacts");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
   for(DataSnapshot datas: dataSnapshot.getChildren()){
    String dept=datas.child("dept").getValue().toString();
      //get other values
   }
}
 @Override
public void onCancelled(DatabaseError databaseError) {
  }
});

First get the userid of the current user, the userid is under the users node in your database, then let the datasnapshot be at contacts node, loop inside of it and get all the values.

Upvotes: 1

Related Questions