无法从Firebase检索数据并显示给用户“ FriebaseRecyclerAdapter”

时间:2019-09-25 00:54:30

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

我的结构:

android studio lint error

我正在致力于社交网络慈善事业的应用当我想从数据库FriebaseRecyclerAdapter中检索数据不起作用时,它显示了一个错误,如下图所示:

enter image description here

我的代码的一部分:

        FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(Posts.class, R.layout.all_posts_layout, PostsViewHolder.class, PostsRef) {
                    @Override
                    protected void onBindViewHolder(@NonNull PostsViewHolder holder, int position, @NonNull Posts model) {
                        holder.setFullname(model.getFullname());
                        holder.setTime(model.getTime());
                        holder.setDate(model.getDate());
                        holder.setDescription(model.getDescription());
                        holder.setProfileimage(getApplicationContext(), model.getProfileimage());
                        holder.setPostimage(getApplicationContext(), model.getPostimage());
                    }

                    @NonNull
                    @Override
                    public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.all_posts_layout, parent, false);
                        PostsViewHolder viewHolder = new PostsViewHolder(view);
                        return viewHolder;
                    }
                };
        postList.setAdapter(firebaseRecyclerAdapter);
    }

1 个答案:

答案 0 :(得分:1)

您需要为其创建查询:-

/**
 * @return object FirebaseRecyclerOptions<Posts> that return only current user posts using current user uid
 */
public FirebaseRecyclerOptions<Posts> queryAllMyPosts() {
    Query myPostQuery = postRef.orderByChild("uid")
            .startAt(current_user_id).endAt(current_user_id + "\uf8ff");
    return new FirebaseRecyclerOptions.Builder<Posts>()
            .setQuery(myPostQuery, Posts.class)
            .build();
}

然后将查询传递给适配器

 /*-- function to display all current user posts --*/
private void displayAllMyPosts() {
    postsAdapter = new PostsAdapter(mPostsController.queryAllMyPosts(), this, new PostsAdapter.ItemClickListener() {

        @Override
        public void onItemClick(String postKey) {
            Intent clickPostIntent = new Intent(MyPostActivity.this, ClickPostActivity.class);
            System.out.println("----key-----" + postKey);
            clickPostIntent.putExtra("postKey", postKey);
            startActivity(clickPostIntent);
        }

        @Override
        public void onLikeButtonClick(final String postKey) {
            mainController.userLikesHandler(postKey);
        }

        @Override
        public void onCommentButtonClick(String postKey) {
            Intent commentsIntent = new Intent(MyPostActivity.this, CommentsActivity.class);
            commentsIntent.putExtra("postKey", postKey);
            startActivity(commentsIntent);
        }
    });
    postsAdapter.startListening();
    myPostsList.setAdapter(postsAdapter);
}