Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Populating ListView from Firebase Database


I have a ListView populated from a Firebase Database:

This is my code:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../comunidades");

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

        }
    };

And this is the Firebase console for this database, branch:"comunidades":

enter image description here

But I am using the same code at another fragment to populate a listview with objects from another branch from the same database, but I get an error.

This is the new code:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../Enclaves");

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


        }
    };

What should I change to get the value for the key "Nombre_enclave" from branch "Enclaves":

enter image description here


Solution

  • What you have under enclaves is not a list of String+String pairs. Instead it's a list of String+Object pairs, with each object having a Comunidad_enclave, Descripcion_enclave, etc.

    The quickest way to make this work is to decide what property you want to show and then override parseSnapshot():

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

    A better way to solve this is to create a class that represents each comunidad. In its simplest form that could look like this:

    class Comunidad {
        public String Comunidad_enclave;
        public String Descripcion__enclave;
        // TODO: the same for the other properties
    }
    

    With this class, you can make your adapter of type Comunidad:

    FirebaseListAdapter<Comunidad> firebaseListAdapter = new FirebaseListAdapter<Comunidad>(
            this.getActivity(),
            String.class,
            android.R.layout.simple_list_item_2,
            databaseReference
    ) {
        @Override
        protected void populateView(View v, Comunidad model, int position) {
            TextView textView1 = (TextView) v.findViewById(android.R.id.text1);
            textView1.setText(model.Comunidad_enclave);
            TextView textView2 = (TextView) v.findViewById(android.R.id.text2);
            textView2.setText(model.Descripcion_enclave);
        }
    };