Firebase DatabaseException无法转换String

时间:2017-01-03 12:43:13

标签: android firebase firebase-realtime-database

我在尝试将一组数据加载到FirebaseRecyclerAdapter

时遇到这个奇怪的错误
  

com.google.firebase.database.DatabaseException:无法转换对象   类型为java.lang.String的类型   com.magn.test.android.model.ProfilesData

参考这个课程:

@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;
}

}

如果我可以将问题缩小到某个功能/方法,那将会更容易,但适配器确实在我完成时打印出数据。它只是没有在布局中结束,应用程序在完成后崩溃。

这是活动:

@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 个答案:

答案 0 :(得分:1)

很简单,我只是在我的数据库中看到一个“孩子”太远了:

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

给了我正确的位置,它可以正确加载一切。检查你的路径!