首页 > 解决方案 > 试图检索用户数据

问题描述

我正在尝试检索用户保存的其他数据,例如他们的帖子、他们上传的图像、描述和帖子的名称,并将其显示在回收站视图中。所有这些信息都保存在一个随机密钥的文件夹中。当我试图检索所有内容时,除了一个空的卡片视图外,什么都没有显示,而且我注意到我收到了这三个警告消息2020-05-06 11:19:50.473 11745-11745/com.myapp.jappy W/ClassMapper: No setter/field for firstname found on class com.myapp.jappy.UserInformation 2020-05-06 11:19:50.474 11745-11745/com.myapp.jappy W/ClassMapper: No setter/field for email found on class com.myapp.jappy.UserInformation 2020-05-06 11:19:50.474 11745-11745/com.myapp.jappy W/ClassMapper: No setter/field for lastname found on class com.myapp.jappy.UserInformation,但我并没有尝试获取用户的电子邮件、名字和姓氏。他们的东西与保存一切的方式有关吗?在此处输入图像描述有人可以帮我解决这个问题。提前致谢。

//Model 

public class Upload {
    private String ImageUrl;
    private String post;
    private String post_name;
    private String description;


    public Upload(String imageUrl, String postname, String postt, String descrip) {
        ImageUrl = imageUrl;
        post = postt;
        post_name = postname;
        description = descrip;




    }

    //Empty constructor needed
    public Upload() {


    }


    public String getImageUrl() {
        return ImageUrl;
    }

    public void setImageUrl(String imageUrl) {
        ImageUrl = imageUrl;
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public String getPost_name() {
        return post_name;
    }

    public void setPost_name(String post_name) {
        this.post_name = post_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}


 //Adapter class

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

    private Context mContext;
    private ArrayList<Upload> users;

    public ImageAdapter(Context context, ArrayList<Upload> uploads){
is        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).getDescription());
        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);
        }
    }
}


//Profile page 

  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();









        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);

        //getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,firstFragment);









            databaseReference.child(uid).addListenerForSingleValueEvent(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);



                    aAdapter.notifyDataSetChanged();







                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();

                }
            });

标签: androidfirebase-realtime-databaseandroid-recyclerview

解决方案


Users Node必须看起来像这样:

Users
|
|------UID
       |
        _____email
        _____firstname
        _____lastname

|------UID2
       ..........
       ..........
       ..........

您现在必须添加一个新节点posts

Upload
|
|------randomID

       |_____ImageUrl
        _____description
        _____post
        _____post_name


|------randomID2
       ..........
       ..........
       ..........

上课Users

public class Users {

//add these
private String email;
private String firstname;
private String lastname;

//constrcutor becomes

public Upload(String email, String firstname, String lastname) {

    this.email = email;
    this.firstname = firstname;
    this.lastname = lastname;


}

//also create getters and setters for email , firstname , lastname..........

您的Upload课程将如下所示:

public class Upload {

private String ImageUrl;
private String post;
private String post_name;
private String description;


//constrcutor 

public Upload(String imageUrl, String postname, String postt, String descrip) {
    ImageUrl = imageUrl;
    post = postt;
    post_name = postname;
    description = descrip;


}



//also create getters and setters for all the fields

推荐阅读