为什么我的数据从Firebase中返回为空(已解决)

时间:2019-07-14 03:22:08

标签: android firebase firebase-realtime-database

我正在尝试从Firebase检索一些数据以填充到我的RecycleView中,但是每次尝试时,我的RecycleView都显示为空。

我已经创建了我的适配器并设置了Recycleview并尝试从Firebase加载数据,但没有任何显示。当我尝试记录信息时,它返回为null。一些帮助,将不胜感激。

这是我的数据库设置方式

 Post
 -Ljl__LUiOJ2YMAK5NHI
 desc: "Product info"
 id:   "sH8LzoaH9UaXahlmssixTpvQy8q2"
 image: "content://media/external/images/media/323211"




 //Retrieves information stored inside Post node...
public void fetchUserInfo() {
    postRef = FirebaseDatabase.getInstance().getReference().child("Post");
    postRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()){
               value = ds.getValue(Post.class);
               postList.add(value);
            }
            adapter = new Adapter(Shop_Activity.this, postList);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();


        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("Error", databaseError.toString());

        }
    });


}

适配器:

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    Context context;
    ArrayList<Post> userPost;

    public Adapter(Context context, ArrayList<Post> userPost){
        this.context = context;
        this.userPost = userPost;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.shop_layout_design,viewGroup, false));

    }

    //this is where you set the value for the ui elements
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.title.setText(userPost.get(i).getDescription());
        viewHolder.description.setText(userPost.get(i).getUserID());
        Picasso.get().load(userPost.get(i).getPostImage()).into(viewHolder.postImage);
    }

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


    //links up ui elements
    class ViewHolder extends  RecyclerView.ViewHolder{

        TextView title;
        TextView description;
        ImageView postImage;

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

            title = itemView.findViewById(R.id.post_title);
            description = itemView.findViewById(R.id.post_desc);
            postImage = itemView.findViewById(R.id.post_image);
        }
    }
}

帖子课

public class Post {

    private String desc;
    private String id;
    private String image;



    public Post(){
    }

    public void setdesc(String desc){
        this.desc = desc;

    }
    public void setimage(String image){
        this.image = image;
    }

    public void setid(String id){
        this.id = id;
    }

    public String getdesc() {
        return desc;
    }
    public String getimage(){
        return image;
    }
    public String getid(){
        return  id;
    }
}

2 个答案:

答案 0 :(得分:0)

我不是Java开发人员,但我可以为您提供帮助。

PoJo ,因为您是数据将是:

public class Post{

    public String Description;
    public String postImage;
    public String userID;


    public String getDescription() {
        return Description;
    }

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

    public String getpostImage() {
        return postImage;
    }

    public void setpostImage(String postImage) {
        this.postImage = postImage;
    }

    public String getuserID() {
        return userID;
    }

    public void setuserID(String userID) {
        this.address = userID;
    }
}

然后,您必须编写适当的代码,这意味着发送和设置值就是您的pojo。我认为您必须将值传递给Adapter。

for(DataSnapshot ds : dataSnapshot.getChildren()){
    Post value = dataSnapshot.getValue(Post.class);
    Post post = new PostModel();
    String Description = value.getDescription();
    String postImage = value.getpostImage();
    String userID = value.getuserID();
    post.setDescription(Description);
    post.setpostImage(postImage);
    post.setuserID(userID);
    postList.add(post);
}
adapter = new Adapter(Shop_Activity.this, postList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();

这种方式可能奏效,因为我使用了reference

答案 1 :(得分:0)

adapter.notifyDataSetChanged();

在设置适配器后添加此