【问题标题】:Can't convert object of type java.lang.String to type com.mycare.OtherClass无法将 java.lang.String 类型的对象转换为 com.mycare.OtherClass 类型
【发布时间】:2018-07-26 12:56:20
【问题描述】:

我查看了其他一些类似问题的答案,但不明白为什么我的问题不起作用。 在我的 firebase 数据库中,我有一个用户列表,其中包含不同的信息。我想从当前的 userId 中检索信息到 recyclerview。

我的代码如下:

  DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference databaseStatus = rootRef.child("other");
    String uid = firebaseAuth.getInstance().getCurrentUser().getUid();
    databaseStatus.child(uid).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot ds : snapshot.getChildren()) {

                for (DataSnapshot dSnapshot : ds.getChildren()) {

                    OtherClass otherClass = dSnapshot.getValue(OtherClass.class);
                    Log.d("Show", otherClass.getOname() == null ? "" : otherClass.getOname());
                    list.add(otherClass);
                }

                adapter = new ShowOtherDetails.OtherAdapter(ShowOtherDetails.this, list);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();
            }
        }

这给了我这个错误:

  com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.mycare.OtherClass

firebase 数据结构如下所示:

请问有人可以帮我吗?

【问题讨论】:

  • 你的快照值是给你一个字符串值。
  • 移除你的userid下的随机id
  • 推送 ID 将来自同一用户的条目存储为唯一的。因为 1 个用户可以向他们的 id 添加多个数据,我希望每个数据都是唯一的,而不是覆盖以前的

标签: java android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

试试这个:

保存时这样做:

    String key = databaseStatus.child(uid).push().getKey()

这样你就可以拿到钥匙了。


然后在第一种方法中在这里检索时将密钥添加为子项

databaseStatus.child(uid).child(key).addValueEventListener(new ValueEventListener() {

   @Override
    public void onDataChange(DataSnapshot snapshot) {



                OtherClass otherClass = snapshot.getValue(OtherClass.class);
                Log.d("Show", otherClass.getOname() == null ? "" : otherClass.getOname());
                list.add(otherClass);
            }

删除for循环for (DataSnapshot ds : snapshot.getChildren())

或者这样做:

    databaseStatus.child(uid).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot snapshot) {

        for (DataSnapshot ds : snapshot.getChildren()) {

            for (DataSnapshot dSnapshot : ds.getChildren()) {

                String date=dataSnapshot.child("date").getValue().toString();
  String detail=dataSnapshot.child("detail").getValue().toString();
  String email=dataSnapshot.child("email").getValue().toString();
  String id=dataSnapshot.child("id").getValue().toString()
   //and so on
            }

【讨论】:

  • 它给了我空值
  • 是的,如我在上面的评论中所说,删除随机 id。 userid下的随机id,不需要。因为只有用户 ID 才能识别记录
  • 还有其他方法吗?因为我希望每个数据都是唯一的
  • 抱歉,回复很长。是的,我尝试了你的第二个解决方案,它对我有用。谢谢
【解决方案2】:
i hope its work, Try this

 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {


                        String date= ds.child("date").getValue(String.class);
                        String detail= ds.child("detail").getValue(String.class);
                        String email= ds.child("email").getValue(String.class);
                        String id= ds.child("id").getValue(String.class);
                        Log.d("TAG", date+ " / "+detail+" / "+email+" / "+id);
                        OtherClass otherClass = new OtherClass(date,detail,email,id);
                        listdata.add(otherClass);

                    }

                    recyclerView.setAdapter(adpter);
                    adpter.notifyDataSetChanged();

                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 2021-07-16
    • 2018-07-08
    • 1970-01-01
    • 2018-05-22
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多