【问题标题】:getting error com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String收到错误 com.google.firebase.database.DatabaseException:无法将 java.util.HashMap 类型的值转换为字符串
【发布时间】:2020-04-29 23:10:20
【问题描述】:

我正在尝试检索用户保存的数据并使用回收站视图将其显示在他们的个人资料页面中。当我运行我的应用程序以查看是否一切正常但我的应用程序不断崩溃并停止工作时。我得到的错误是com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String。我以前有这个工作,但我不确定现在是什么问题。谁能帮我解决这个问题。提前致谢。 Updated database

图像适配器

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{

    private Context mContext;
    private ArrayList<UserInformation> users;

    public ImageAdapter(Context context, ArrayList<UserInformation> uploads){
        mContext = context;
        users = uploads;

    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View V = LayoutInflater.from(mContext).inflate(R.layout.cardview, parent, false);
        return new ImageViewHolder(V);


    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        //String uploadCurrent=users.get(position).getmImageUrl();

        holder.txt1.setText(users.get(position).getAction());
        Picasso.get().load(users.get(position).getmImageUrl()).fit().centerCrop().into(holder.imageView);

    }

    @Override
    public int getItemCount() {
        return users.size();

    }

    public class ImageViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageView;
        public TextView txt1;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView=itemView.findViewById(R.id.imageview);
            //txt1=itemView.findViewById(R.id.action);
        }
    }
}

模型类

  public UserInformation(String  mImageUrl,  String cate, String action, String headline, String websiteurl) {

        this.mImageUrl =  mImageUrl;
        this.cate = cate;
        this.action = action;
        this.headline = headline;
        this.websiteurl = websiteurl;

    }

    public UserInformation() {

    }

    public String getmImageUrl() {
        return mImageUrl;
    }

    public void setmImageUrl(String mImageUrl) {
        this.mImageUrl = mImageUrl;
    }


    public String getCategory() {
        return cate;
    }

    public void setCategory(String category) {
        this.cate = cate;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getHeadline() {
        return headline;
    }

    public void setHeadline(String header) {
        this.headline = headline;
    }

    public String getWebsiteurl() {
        return websiteurl;
    }

    public void setWebsiteurl(String websiteurl) {
        this.websiteurl = websiteurl;
    }
}

主类

recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),3);

recyclerView.setLayoutManager(layoutManager);
myUploads = new ArrayList<UserInformation>();
aAdapter = new ImageAdapter(ProfileActivity.this, myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();

h=new Handler();

databaseReference = FirebaseDatabase.getInstance().getReference("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();

firebaseUser = firebaseAuth.getInstance().getCurrentUser();
t=findViewById(R.id.none);

    databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {

                UserInformation upload=postsnapshot.getValue(UserInformation.class);


                myUploads.add(upload);
                 aAdapter = new ImageAdapter(ProfileActivity.this, myUploads);
                 recyclerView.setAdapter(aAdapter);

            }

【问题讨论】:

  • 将数据库屏幕截图添加到您的问题中,而不是作为链接。

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


【解决方案1】:

据我所知,您的阅读代码归结为:

databaseReference = FirebaseDatabase.getInstance().getReference("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {

            UserInformation upload=postsnapshot.getValue(UserInformation.class);


            myUploads.add(upload);
             aAdapter = new ImageAdapter(ProfileActivity.this, myUploads);
             recyclerView.setAdapter(aAdapter);

        }

这意味着您将一个侦听器附加到数据库中的/Users/$uid,并且您的onDataChange 将使用单个用户的数据快照进行调用。

在这种情况下,您不需要在 onDataChange 中循环遍历 snapshot.getChildren(),因为您已经将侦听器作为目标来读取精确路径上的单个快照。所以:

databaseReference = FirebaseDatabase.getInstance().getReference("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        UserInformation upload = dataSnapshot.getValue(UserInformation.class);

        myUploads.add(upload);
        aAdapter = new ImageAdapter(ProfileActivity.this, myUploads);
        recyclerView.setAdapter(aAdapter);
    }

【讨论】:

  • 范普芬感谢您的回复。我按照你告诉我的去做,但现在没有像保存的图像那样显示它只是一个没有任何内容的卡片视图
  • 这比崩溃要好,所以我会考虑这个过程。 :) 如果您想查看代码更改是否有效,请在原始代码和更新之间的UserInformation upload = dataSnapshot.getValue(UserInformation.class) 行上放置一个断点。如果upload 在更新中该行之后有值,则它能够从数据库中读取值,下一个问题在更下游。
  • 您在数据库中存储mImageUrl 的方式使您更难读回它。这是一个单独的问题,所以我建议要么查看here,要么专门为此打开一个新问题。一般来说,你应该努力隔离你的问题,以至于我的回答要么是对的要么是错的,而不是像这里的情况那样部分正确。
  • 抱歉耽搁了。我仍然有这个问题。图像没有显示,我只是得到一个空白的卡片视图。我什至修复了 mImageUrl 部分的存储方式,您可以查看上面更新的数据库。我不确定是什么问题?
猜你喜欢
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 2018-01-07
相关资源
最近更新 更多