Search code examples
javaandroidlistviewfirebase-realtime-databasefirebaseui

Populate ListView with Firebase Adapter


I'm receiving these errors when I tried to populate a list view with firebase adapter using firebase UI

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String com.google.firebase.database.DatabaseException: Class java.util.HashMap has generic type parameters, please use GenericTypeIndicator instead

Here Is the code

     DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
     DatabaseReference a = ref.child("info");

        final FirebaseListAdapter<String> adapter =
              new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,a) {
                  @Override
                  protected void populateView(View v, String model, int position) {

                      TextView text = (TextView) findViewById(android.R.id.text1);
                      text.setText(model);

                  }

here is an example of json data

 "info" : {
   "KSTUJILdwPN305Fs7ujhga4knlG3" : {
     "File Info" : {
       "-JFRkA855rfOU7GtcK4" : {
         "Name" : "John",
         "Adress" : "Test Adress",
         "Favourite_food" : "Bread",
       },

Solution

  • info node does not refer to your data model. It may contain children nodes. So to reach the model, you should use a reference like this.

     DatabaseReference a = ref.child("info").child(info_id).child("File Info").child(file_id); 
    

    and you should have a FileInfo model instead of String model to use in

    populateView(View v, FileInfo model, int position):
    

    and model

    public class FileInfo {
    
    private String Name;
    private String Adress;
    private String Favourite_food;
    
    public FileInfo() {
    }
    
    public FileInfo(String Name, String Adress, String Favourite_food) {
        this.Name = Name;
        this.Adress = Adress;
        this.Favourite_food = Favourite_food;
    }
    
    public String getName() {
        return Name;
    }
    
    public void setName(String Name) {
        this.Name = Name;
    }
    
    public String getAdress() {
        return Adress;
    }
    
    public void setAdress(String Adress) {
        this.Adress = Adress;
    }
    
    public String getFavourite_food() {
        return Favourite_food;
    }
    
    public void setFavourite_food(String Favourite_food) {
        this.Favourite_food = Favourite_food;
    }
    
    }