RecyclerView不显示来自Firebase数据库的信息

时间:2020-08-25 10:06:21

标签: android android-recyclerview

我的recyclerview没有显示任何信息。 recyclerview工作正常,但没有任何信息,它完全空白。而且布局是空白的...

logcat中没有错误...

我尝试执行以下操作:

1]在xml中设置高度以包装内容。

2]设置notifySetChangeListener。

3]实现startListening。

这些是其他人提出的同一问题的大部分答案,但是即使已经全部实施,recyclerView还是空白。

我的MainActivity:

          @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

         postList = (RecyclerView) findViewById(R.id.all_users_post_list);
    postList.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    postList.setLayoutManager(linearLayoutManager);

     DisplayAllUsersProfile();}


      private void DisplayAllUsersProfile() {

    FirebaseRecyclerOptions<postsRecyclerView> options =
            new FirebaseRecyclerOptions.Builder<postsRecyclerView>()
                    .setQuery(PostsRef  , postsRecyclerView.class)
                    .build();

    FirebaseRecyclerAdapter<postsRecyclerView,PostsViewHolder>
            firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<postsRecyclerView, PostsViewHolder>(options) {

        @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;
        }

        @Override
        protected void onBindViewHolder(@NonNull final PostsViewHolder holder, int position, @NonNull postsRecyclerView model) {

            holder.textUsername.setText(model.getUsername());
            holder.textContent.setText(model.getContent());
            holder.textTitle.setText(model.getTitle());
            holder.textDate.setText(model.getDate());
            holder.textTime.setText(model.getTimestamp());

            holder.LikePostButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LikeChecker  =  true;
                    LikesRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
                }
            });
        }
    };
    postList.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();
    firebaseRecyclerAdapter.notifyDataSetChanged();
}



public class PostsViewHolder extends RecyclerView.ViewHolder {

    TextView textUsername, textTitle, textContent, textDate, textTime;
    TextView DisplayNoOfLikes;
    ImageButton LikePostButton, CommentPostButton;

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

        LikePostButton = (ImageButton) itemView.findViewById(R.id.like_button);
        CommentPostButton = (ImageButton) itemView.findViewById(R.id.comment_button);
        DisplayNoOfLikes = (TextView) itemView.findViewById(R.id.display_no_of_likes);

        textUsername = itemView.findViewById(R.id.post_user_name);
        textTitle = itemView.findViewById(R.id.post_title);
        textContent = itemView.findViewById(R.id.post_article);
        textDate = itemView.findViewById(R.id.post_date);
        textTime = itemView.findViewById(R.id.post_time);
    }
}

我的模型班:

      public class postsRecyclerView  {

String username;
String content;
String title;
String date;
String timestamp;

public postsRecyclerView(String username, String content, String title, String date, String timestamp) {
    this.username = username;
    this.content = content;
    this.title = title;
    this.date = date;
    this.timestamp = timestamp;
}

public postsRecyclerView() {
}

public String getTimestamp() {
    return timestamp;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

我的Firebase数据库: Firebase database

我真的需要你的帮助...

我在这里住了好几天...

谢谢......

2 个答案:

答案 0 :(得分:0)

尝试使用

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

答案 1 :(得分:0)

要使其运行,您可以尝试以下解决方案:

PostsRef = FirebaseDatabase.getInstance().getReference().child("Notes").child("..."); 
// ... is for those Ids of notes

我还认为startListening()应该在setAdapter(...)之前

firebaseRecyclerAdapter.startListening();
postList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();