'How Can I Hide Button On Some Recycler View Rows For Only Some Users

Issue:

  • Currently, a user can write a post and it displays in a recycler view.

  • There is a menu button on each recycler view row where a user can edit/delete their post.

  • I don't want users who are not the authors of the post to be able to access this menu button.

  • I want the menu button hidden for everyone who is not the author of that post.

  • Below is part of my adapter

  • I know I'm probably missing something very simple, but I just can not figure out what it is. Thank you so much for your help.

{
    private ArrayList<MainWallModel> mainwallPosts = new ArrayList<>();
    private MainWallInterface mainWallInterface;
    FirebaseAuth mAuth;
    private FirebaseUser currentuser;

    private String userID, currentUserID;

    public  void updateMainPosts(ArrayList list) {
        //this is used to update the adapter when new data is added, so it will show fresh data in the recyclerview.//
        mainwallPosts .clear();
        mainwallPosts .addAll(list);
        notifyDataSetChanged();
    }



    public MainPostsAdapter(@NonNull FirebaseRecyclerOptions<MainWallModel> options) {
        super(options);
        currentuser = FirebaseAuth.getInstance().getCurrentUser();

    }

    @Override
    protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull MainWallModel model)
    {

        //MainWallModel mainWallModel = mainwallPosts.get(position);
        mAuth = FirebaseAuth.getInstance();
        String currentUserId = mAuth.getCurrentUser().getUid();

        holder.full_name.setText(model.getFirst_name());
        holder.post_date.setText(model.getPost_date());
        holder.post_body_text.setText(model.getPost_body_text());

        holder.setData(model,mainWallInterface);

/////////POINT OF ISSUE - APP CRASHES //////////////

        String postAuthorId = model.getUser_id();
        if(postAuthorId.equals(currentUserId)){
            holder.kabobMenuButton.setVisibility(VISIBLE);

        } else {
            holder.kabobMenuButton.setVisibility(GONE);

        }
/////////POINT OF ISSUE - APP CRASHES //////////////



Solution 1:[1]

I solved this issue (it was silly).

  • I went into the Firebase database and noticed that there were some entries without a User Id.
  • These entries were already in there database before i started pushing user ids to the particular node.
  • I just deleted these bad entries and re-ran the app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 greenTea