简体   繁体   English

Firebase 实时数据库 - 检索数据 Android Studio

[英]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 实时数据库

FirebaseRealtime 数据库结构

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.

相关问题 从 Android Studio 中的实时 firebase 数据库中检索数据 - Retrieve data from realtime firebase database in Android Studio 如何使用 Android Studio 从 Firebase 实时数据库中的节点检索特定数据? - How to retrieve a specific data from a node in Firebase Realtime Database using Android Studio? 如何使用 Android 在 Firebase 实时数据库中检索经过身份验证的用户的用户数据 - How to retrieve user data of authenticated users in Firebase Realtime Database with Android 从 Android 中的 Firebase 实时数据库检索 ListView 上的数据不起作用 - retrieve data on ListView from Firebase Realtime Database in android not working 如何从 Android Firebase 实时数据库中的节点检索所有数据? - How to retrieve all the data from a node in Android Firebase realtime database? 无法从 Firebase 实时数据库 Android 检索数据 - Cannot retrieve data from Firebase Realtime Database Android Java, Android studio, Firebase 数据库如何将数据添加到实时数据库 - Java, Android studio, Firebase database how to add data to realtime database 从Firebase实时数据库,Android Studio,Java获取数据 - Getting Data from Firebase realtime database, Android Studio, Java Android Studio 未从 Firebase 实时数据库读取检索到的数据 - Android Studio is not reading the retreived data from Firebase realtime database 在 Android Studio 中使用 push() 在 Firebase 实时数据库中添加数据 - Adding data in Firebase realtime database using push() in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM