Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Error "Can't convert object of type" in custom FirebaseListAdapter


My app uses custom FirebaseListAdapter. I have created AreaGridAdap.java,

public class AreaGridAdap {

    String gridArea;

    public AreaGridAdap(){

    }

    public AreaGridAdap(String gridArea) {
        this.gridArea = gridArea;
    }


    public String getGridArea() {
        return gridArea;
    }
}

this is my firebaseListAdapter,

final FirebaseListAdapter<AreaGridAdap> firebaseListAdapter = new FirebaseListAdapter<AreaGridAdap> (AreaGridActivity.this,
                AreaGridAdap.class,
                R.layout.area_grid,
                databaseReference) {
            @Override
            protected void populateView(View v, AreaGridAdap model, int position) {

                areaTextView = (TextView) v.findViewById(R.id.areaGridText);

                areaTextView.setText(model.getGridArea());
            }
        };

For the first time I have used default FirebaseListAdapter and it worked fine. But when I use custom FirebaseListAdapter, my app crashes and I got this as my error message:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type trickandroid.fulllogin.AreaGridAdap

Help me solve this error.


Solution

  • This error message means that the data structure under databaseReference doesn't match your code. It looks like you have a data structure like this:

    -K....1: "First value"
    -K....2: "Second value"
    -K....3: "Third value"
    

    While your code is made to handle this structure:

    -K....1: {
      gridArea: "First value"
    }
    -K....2: {
      gridArea: "Second value"
    }
    -K....3: {
      gridArea: "Third value"
    }