Search code examples
androidfirebaselistviewfirebase-realtime-databasefirebaseui

Firebase Database populate child data into ListView using FirebaseUI


I want to populate a child data in Firebase Realtime Database into ListView using FirebaseUI.

My Database structure is like this.

- Personnel
    - 00
       - Name: "Wallace"
       - DOB: "11/21/87"
    - 01
       - Name: "Carl"
       - DOB: "05/14/90"
    - 02
       - Name: "Johnson"
       - DOB: "01/17/89"

I currently using this to populate data into ListView

mListView = (ListView) rootView.findViewById(R.id.listview1);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-project-here.firebaseio.com/personnel");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(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);
    }
};
mListView.setAdapter(firebaseListAdapter);

And the list is displayed as: (Yes it's fine but not in my format)

 00
----
 01
----
 02

I want them to display the first(or specific) child of every data like this:

 Wallace
---------
 Carl
---------
 Johnson

Is there any way to implement to make this working? Thanks!

Notes

  • This ListView is in Fragment
  • My FirebaseUI Database is version 1.0.1 com.firebaseui:firebase-ui-database:1.0.1

Thanks again!


Solution

  • Create a tiny Java class to represent your users:

    public class Personnel {
      public String Name;
      public String DOB;
    }
    

    And then wire this up to the adapter in your code:

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