我的结构:
我正在致力于社交网络慈善事业的应用当我想从数据库FriebaseRecyclerAdapter
中检索数据不起作用时,它显示了一个错误,如下图所示:
我的代码的一部分:
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);
}
答案 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);
}