[英]Firebase Realtime Database - retrieve data Android Studio
Getting error: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.ken.careerapp.Models.Jobs
获取错误:com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.ken.careerapp.Models.Jobs
Using a fragment below:使用下面的片段:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.job_listings_fragment, container, false);
recyclerView = view.findViewById(R.id.recyclerviewjob);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerAdapterJob = new RecyclerAdapterJob(jobs,JobFragment.this::onJobClick);
recyclerView.setAdapter(recyclerAdapterJob);
jobs = new ArrayList<Jobs>();
//initData();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Jobs");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
Jobs data = ds.getValue(Jobs.class);
jobs.add(data);
}
recyclerAdapterJob.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//recyclerView.setAdapter(new RecyclerViewAdapter2(initData()));
return view;
}
The job model class which is passed to getValue in datasnapshot above:在上面的数据快照中传递给 getValue 的作业模型类:
public class Jobs {
private String description;
private String location;
private String deadline;
public Jobs() {
}
public Jobs(String description, String location, String deadline) {
this.description = description;
this.location = location;
this.deadline = deadline;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDeadline() {
return deadline;
}
public void setDeadline(String deadline) {
this.deadline = deadline;
}
} }
FirebaseRealtime database Firebase 实时数据库
Give a photo or your realtime database architecture.给一张照片或你的实时数据库架构。 that will help much to ans this issue.
这将有助于解决这个问题。
You are getting the following error:您收到以下错误:
Getting error: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.ken.careerapp.Models.Jobs
获取错误:com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.ken.careerapp.Models.Jobs
Because you are trying to convert a String, into an object of type Jobs, and this actually not possible in Java.因为您正在尝试将 String 转换为 Jobs 类型的对象,而这在 Java 中实际上是不可能的。 As I see in your screenshot, under the
Jobs
node, there is only a single Jobs object that contains exactly three String properties.正如我在您的屏幕截图中看到的,在
Jobs
节点下,只有一个 Jobs 对象恰好包含三个 String 属性。 So when you iterate that node, you get String objects back and not Jobs objects, hence that error.因此,当您迭代该节点时,您会返回 String 对象而不是 Jobs 对象,因此会出现该错误。
To solve this, you should create multiple objects under that node, as I understand, you want to display them in a RecyclerView.要解决这个问题,您应该在该节点下创建多个对象,据我所知,您希望将它们显示在 RecyclerView 中。 To solve this, you should use the push() method, when you add those jobs to the database.
要解决这个问题,您应该在将这些作业添加到数据库时使用 push() 方法。 You didn't share that part of code, but I imagine that looks similar to this:
你没有分享那部分代码,但我想这看起来类似于:
databaseReference = FirebaseDatabase.getInstance().getReference().child("Jobs");
databaseReference.setValue(jobs);
In which jobs
is an object of type Jobs.其中,
jobs
是一个 Jobs 类型的对象。 This is not correct, as it produces that error when reading data.这是不正确的,因为它在读取数据时会产生该错误。 To solve this, please change the above line of code to:
要解决这个问题,请将上面的代码行更改为:
databaseReference.push().setValue(jobs);
In this case, your new structure will look like this:在这种情况下,您的新结构将如下所示:
Firebase-root
|
--- Jobs
|
--- pushedId
|
---deadline: "September"
|
---description: "Intern Software Developer"
|
---location: "Moi Avenue Street"
And the rest of the code should work.其余的代码应该可以工作。
PS聚苯乙烯
Don't ignore potential errors!不要忽视潜在的错误!
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage());
}
Always log the error message.始终记录错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.