Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Can't populate the keys from firebase into FirebaseListAdapter


i want to retrieve the keys into FirebaseListAdapter not the values like this example: screenshot_1

so i want to show "Cat" and "Dog" in the listview but i don't know how to do this cuz i searched a lot but found nothing.


Solution

  • The default implementation of FirebaseListAdapter calls getValue() with the class you specified when constructing the adapter. By overriding parseSnapshot you can determine what part of each DataSnapshot is handed to populateView(). To get the key:

    FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
            this,
            String.class,
            android.R.layout.simple_list_item_1,
            databaseReference
    ) {
         @Override
         protected String parseSnapshot(DataSnapshot snapshot) {
             return snapshot.getKey();
         }
    
        @Override
        protected void populateView(View v, String model, int position) {
            TextView textView = (TextView) v.findViewById(android.R.id.text1);
            textView.setText(model);
        }
    };