Firebase DatabaseException无法转换String类型的对象 [英] Firebase DatabaseException Can't convert object of type String
问题描述
FirebaseRecyclerAdapter
$时出现这个奇怪的错误b $ bcom.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;
$ b 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 =死亡;
this.id = id;
$ b public String getForename(){
return forename;
}
public void setForename(String forename){
this.forename = forename;
}
public String getSurname(){
return surname;
$ b $ 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;
code
$ b $ p
$ b 如果我可以将问题的范围缩小到某个函数/方法,那将会更容易,但是适配器在完成时会将数据打印出来。它只是不会在布局结束,应用程序完成后崩溃。
这是活动:
@Override
保护无效的onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection);
recyclerView =(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(新的LinearLayoutManager(this));
mAuth = FirebaseAuth.getInstance();
mAuth.addAuthStateListener(this);
//初始化数据库
user = FirebaseAuth.getInstance()。getCurrentUser();
database = FirebaseDatabase.getInstance()。getReference()。child(users)。child(user.getUid())。child(profiles_info)。child(1);
@覆盖
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());
}
};
//滚动到新消息的底部
firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver(){
@Override
public void onItemRangeInserted(int positionStart,int itemCount ){
}
});
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
$ b / **
* ViewHolder在将profiledata加载到适配器时应用图形更改:
* /
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);
$ b
解决方案
$ p $ database = FirebaseDatabase.getInstance() .getReference()子( 用户)子(user.getUid())子( profiles_info)。;
给了我正确的位置,它可以正确加载一切。检查你的路径!
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);
}
}
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!
这篇关于Firebase DatabaseException无法转换String类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!