android - 无法将字符串转换为 DjProfile(firebase)
问题描述
我试图从我的 json 中获取数据以通过 firebase recyclerview 显示,但我遇到了这个错误:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.android.heydj.DjProfile
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.6:423)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.6:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.6:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.6:212)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
我的杰森:
"allDeeJays" : {
"-Le5FguoZsnT2lejgAkZ" : "yousucks",
"-Le5IsCPK69NTbgbgUUo" : "ffffggg",
"-Le5J5CmPkddSaK_MgpG" : "ahhhaahhaahha"
}
我的 DjProfile 类定义如下:
public class DjProfile
{
String djName;
String googleUserName;
public DjProfile(String djName, String googleUserName)
{
this.djName = djName;
this.googleUserName = googleUserName;
}
public DjProfile(){}
public void setdjName(String djName)
{
this.djName = djName;
}
public String getdjName()
{
return djName;
}
}
我的firebase recyclerview如下:
query = mProfileDatabase.child("allDeeJays")
.orderByValue()
.startAt(t.getText().toString()).endAt(t.getText().toString() + "\uf8ff");
FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
.setQuery(query, DjProfile.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{
@NonNull
@Override
public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
return new ResultsViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ResultsViewHolder holder, final int position, @NonNull final DjProfile model) {
holder.setDjProfile(model);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplication(), AddSongRequest.class);
i.putExtra("DjName", model.getdjName());
startActivity(i);
}
});
}
};
firebaseRecyclerAdapter.startListening();
Firebase recyclerview 的 ViewHolder 类:
public static class ResultsViewHolder extends RecyclerView.ViewHolder
{
private TextView djNameView;
public ResultsViewHolder(View itemView)
{
super(itemView);
djNameView = itemView.findViewById(R.id.result_dj);
}
void setDjProfile(DjProfile profile)
{
String djName = profile.getdjName();
profile.setdjName(djName);
}
}
任何帮助表示赞赏:)
编辑 我的 JSON 现在格式化为:
"allDeeJays" : {
"-LeNGZKDoIcVOLUokvrP" : {
"djName" : "ahhahaha"
},
"-LeNGb2sy9qG_U0zA1xS" : {
"djName" : "newdj"
}
}
现在我无法使用以下方法将 name 的值显示到 recyclerview 中:
query = mProfileDatabase.child("allDeeJays")
.orderByValue()
.startAt(t.getText().toString()).endAt(t.getText().toString() + "\uf8ff");
解决方案
更改您的数据库如下:
allDeeJays
PushId
djName : "john"
googleUserName : "john"
然后您就可以将您的 POJO 与数据库进行映射。
推荐阅读
- salt-stack - 如何执行 sls 规则并采取有条件的行动?
- c++ - 如何从列表中获得最可能的对
- sql - 如何从历史记录表中选择未删除的记录?
- java - 如何从部署在 docker 上的应用程序连接到外部服务?
- postgresql - 如何修复 psql shell Windows 中“psql”处或附近的语法错误
- swift - 如何在 SwiftUI 中的 PageViewController 中传递一些视图?
- assembly - 理解 rol/ror 伪指令 mips
- javascript - 将选择框包装到自定义元素中
- python - 在另一个变量中搜索特定字符串
- javascript - 有没有办法使用预制的“气泡”类一次无休止地将一个气泡添加到画布上?