1

I'm trying to get the unique child name but its always returning the same id or it's returning what I enter in quotes for child.

uid hold "users" instead of traveling into users uid hold "users" instead of traveling into users

Im trying to get the child name under users which is the long key/string: Im trying to get the child name under users which is the long key/string

public class displayUserBooks extends AppCompatActivity {

ListView listViewBooks;
List<existingBooks> existingBookses;
private FirebaseAuth mAuth;
FirebaseDatabase databaseViewbooks = FirebaseDatabase.getInstance();
DatabaseReference myRef = databaseViewbooks.getReference("Books");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_user_books);
    mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    listViewBooks= (ListView) findViewById(R.id.listViewBooks);
    existingBookses = new ArrayList<>();


}
@Override
protected void onStart() {
    super.onStart();
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            checkBooks(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void checkBooks(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        String uid=ds.child("users").getKey();
        existingBooks eBooks = new existingBooks();
        eBooks = ds.getValue(existingBooks.class);
         //   if(user.getUid().equals(uid))
            existingBookses.add(eBooks);
    }

    Booklist adapter = new Booklist(displayUserBooks.this,existingBookses);
    listViewBooks.setAdapter(adapter);
}}
AdishRao
  • 163
  • 1
  • 1
  • 12
  • Duplicate? https://stackoverflow.com/q/40101010/1531971 –  Sep 22 '17 at 18:53
  • @jdv When I type .getKey() i only get the outer key (first child) and not the users keys (users is a child of the first child) – AdishRao Sep 22 '17 at 19:02
  • You obviously need a different approach – Chisko Sep 22 '17 at 19:08
  • Don't paste pictures of text. Instead post the actual code or JSON as text. For the JSON you can easily get this by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON and code as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Sep 22 '17 at 19:10
  • @Chisko Any suggestions? – AdishRao Sep 22 '17 at 19:30

1 Answers1

0

enter image description herethe root cause most likely is in the construction of the reference to the child node. It may be defined "too deep". Please refer to the snapshot of hierarchical picture of the DB tree, and portion of working code below. Inside the listener, it's traveling into the data of every user (defined by user ID):

FirebaseDatabase frDb = FirebaseDatabase.getInstance();
        DatabaseReference frDbRef = frDb.getReference("apps/gastracking").child("users").child(mAuth.getCurrentUser().getUid());
        ChildEventListener chEvLst = frDbRef.addChildEventListener(new ChildEventListener() {
            ArrayList<RawReport> listOfRawreports = new ArrayList<RawReport>();

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.getChildrenCount() > 0) {
                    for (DataSnapshot ds1 : dataSnapshot.getChildren()) 

//<...... Put your code here.....>

}

} }

Hierarchical structure of the DB nodes: unfortunately I can't add flat snapshot - so please refer to the link at the beginning of my answer.

Cadet
  • 376
  • 1
  • 9