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

问题描述 投票:0回答:1

我的结构:

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);
    }
java android firebase firebase-realtime-database firebaseui
1个回答
0
投票

您需要为其创建查询:-

/**
 * @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);
}
© www.soinside.com 2019 - 2024. All rights reserved.