Recycler View does not show data

Recycler view does not read or show data from the firebase database, for two days now I think I ask you for help. the problem is definitely not in the Firebase database because the data is being written successfully, but with the output of the problem.It seems to me that the problem is in onStart, or rather in connecting the adapter to the Recycler View

    public class HomeActivity extends RootActivity {
      FirebaseAuth mAuth;
      private FloatingActionButton fab;
      private MaterialCardView PostCard;
      private MaterialToolbar topAppBar;
      private NavigationView nav_view;
      private LottieAnimationView serach;
      private TextView userName, userLogin;
      private RecyclerView PostRV;
      private DrawerLayout drawerLayout;
      Context context;
      PostAdapter adapter;
      private ImageView userPick;
      DatabaseReference db;
      FirebaseDatabase firebaseDatabase;
      FirebaseStorage fs;
      StorageReference sr;
      FirebaseUser currentUser;
      List<Post> postList;


      @Override
      protected void onStart() {
          super.onStart();
          db.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                  postList = new ArrayList<>();
                  for (DataSnapshot postsnap : snapshot.getChildren()) {
                      Post post = postsnap.getValue(Post.class);
                      postList.add(post);
                  }

                  adapter = new PostAdapter(context, postList);
                  PostRV.setAdapter(adapter);
              }


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

              }
          });
      }
      

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


          PostRV = (RecyclerView)

                  findViewById(R.id.PostRV);

          nav_view = (NavigationView)

                  findViewById(R.id.nav_view);

          drawerLayout = (DrawerLayout)

                  findViewById(R.id.drawerLayout);

          fab = (FloatingActionButton)

                  findViewById(R.id.fab);

          topAppBar = (MaterialToolbar)

                  findViewById(R.id.topAppBar);


          PostRV.setLayoutManager(new

                  LinearLayoutManager(HomeActivity.this));
          firebaseDatabase = FirebaseDatabase.getInstance();
          db = firebaseDatabase.getReference("Posts");







          topAppBar.setNavigationOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  drawerLayout.openDrawer(GravityCompat.START);
              }
          });


          mAuth = FirebaseAuth.getInstance();
          currentUser = mAuth.getCurrentUser();


          FirebaseStorage fr = FirebaseStorage.getInstance();


          mAuth = FirebaseAuth.getInstance();
          currentUser = mAuth.getCurrentUser();
          View hl = nav_view.getHeaderView(0);
          TextView userName = hl.findViewById(R.id.userName);
          TextView userLogin = hl.findViewById(R.id.userLogin);
          ImageView userPick = hl.findViewById(R.id.userPick);


          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(HomeActivity.this, AddActivity.class);
                  startActivity(intent);
              }
          });
      }
  }

PostAdapter

public  class  PostAdapter extends RecyclerView.Adapter<PostAdapter.MyViewHolder>{


Context mContext;
List<Post> mData;
public PostAdapter(Context mContext, List<Post> mData) {
    this.mContext = mContext;
    this.mData = mData;
}

@NonNull
@NotNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    View row = LayoutInflater.from(mContext).inflate(R.layout.row_signle_post,parent,false);

    return new MyViewHolder(row);
}

@Override
public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {
    holder.tvTitle.setText(mData.get(position).getTitle());
    holder.tvTitle.setText(mData.get(position).getUsername());
    Glide.with(mContext).load(mData.get(position).getUserPick()).into(holder.imgUserPick);
    Glide.with(mContext).load(mData.get(position).getPostImage()).into(holder.imgPost);

; }

@Override
public int getItemCount() {
    return 0;
}


public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView tvTitle;
    TextView tvName;
    ImageView imgPost;
    TextView tag;
    CircleImageView imgUserPick;


    public MyViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);


        tvTitle = itemView.findViewById(R.id.title);
        tvName = itemView.findViewById(R.id.username);
        tag = itemView.findViewById(R.id.tag);
        imgPost = itemView.findViewById(R.id.imgPost);
        imgUserPick = itemView.findViewById(R.id.userPick);





    }
}

Post model

ublic class Post {
private String title;
private String username;
private String postImage;
private String tag;
private String userId;
private String userPick;
private String description;
private String location;
private String wallet;
private Object time_stamp;


private String postKey;



public Post(String title, String username, String postImage, String tag, String userId, String userPick, String description, String location, String wallet) {
    this.username = username;
    this.title = title;
    this.tag = tag;
    this.userId = userId;
    this.userPick = userPick;
    this.description = description;
    this.wallet = wallet;
    this.postImage = postImage;
    this.location = location;
    this.time_stamp = DocumentTransform.FieldTransform.ServerValue.REQUEST_TIME;



}

public Post() {
}


public String getPostKey() {
    return postKey;
}

public void setPostKey(String postKey) {
    this.postKey = postKey;
}

public String getTitle() {
    return title;
}

public String getUsername() {
    return username;
}

public String getPostImage() {
    return postImage;
}

public String getTag() {
    return tag;
}

public String getUserId() {
    return userId;
}

public String getUserPick() {
    return userPick;
}



public String getDescription() {
    return description;
}

public String getLocation() {
    return location;
}

public String getWallet() {
    return wallet;
}


public Object getTime_stamp() {
    return time_stamp;
}

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

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

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

public void setTag(String tag) {
    this.tag = tag;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public void setUserPick(String userPick) {
    this.userPick = userPick;
}

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

public void setLocation(String location) {
    this.location = location;
}

public void setWallet(String wallet) {
    this.wallet = wallet;
}

public void setTime_stamp(Object time_stamp) {
    this.time_stamp = time_stamp;
}

}



Read more here: https://stackoverflow.com/questions/67935791/recycler-view-does-not-show-data

Content Attribution

This content was originally published by Ruslan Razumovsky at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: