0

I'm getting this strange error when trying to load a set of data into the FirebaseRecyclerAdapter

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.magn.test.android.model.ProfilesData

Referring to this class:

@IgnoreExtraProperties
public class ProfilesData {

public String forename;
public String surname;
public String quote;
public String birth;
public String demise;
public String id;


public ProfilesData(){

}

public ProfilesData(String forename, String surname, String quote, String birth, String demise, String id){
    this.forename = forename;
    this.surname = surname;
    this.quote = quote;
    this.birth = birth;
    this.demise = demise;
    this.id = id;
}



public String getForename() {
    return forename;
}

public void setForename(String forename) {
    this.forename = forename;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getQuote() {
    return quote;
}

public void setQuote(String quote) {
    this.quote = quote;
}

public String getBirth() {
    return birth;
}

public void setBirth(String birth) {
    this.birth = birth;
}

public String getDemise() {
    return demise;
}

public void setDemise(String demise) {
    this.demise = demise;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

It would've been easier if I could narrow the issue down to a certain function/method, but the adapter does fetch the data as I print it out when it's done. It just doesn't end up in the layout, and the app crashes short after it is done.

Here's the Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selection);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    mAuth = FirebaseAuth.getInstance();
    mAuth.addAuthStateListener(this);

    //Initialize database
    user = FirebaseAuth.getInstance().getCurrentUser();
    database = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid()).child("profiles_info").child("1");


}


@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

}



@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<ProfilesData, ProfilesViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ProfilesData, ProfilesViewHolder>(
            ProfilesData.class, R.layout.list_item, ProfilesViewHolder.class, database) {
        @Override
        protected void populateViewHolder(ProfilesViewHolder viewHolder, ProfilesData model, int position) {

            System.out.println("NAME ARE: " + model.getForename());

            viewHolder.setName(model.getForename() + " " + model.getSurname());
            viewHolder.setDates(model.getBirth() + " " + model.getDemise());


        }
    };

    // Scroll to bottom on new messages
    firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {

        }
    });

    recyclerView.setAdapter(firebaseRecyclerAdapter);

}







/**
 * ViewHolder to apply graphical changes when loading profiledata into adapter:
 */

public static class ProfilesViewHolder extends RecyclerView.ViewHolder {

    private final TextView name_tv;
    private final TextView date_tv;

    public ProfilesViewHolder(View itemView){
        super(itemView);
        name_tv = (TextView) itemView.findViewById(R.id.profile_name_tv);
        date_tv = (TextView) itemView.findViewById(R.id.profile_dates_tv);
    }

    public void setName(String name){
        name_tv.setText(name);
    }

    public void setDates(String dates){
        date_tv.setText(dates);
    }

}
1

1 Answer 1

1

Simple enough I simply was looking one "child" too far in my database:

database = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid()).child("profiles_info"); 

gave me the correct location and it could load everything correctly. Check your paths!

Sign up to request clarification or add additional context in comments.

2 Comments

I have to wait a set of hours before I can do that.
"Check your paths" is perfect answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.