Search code examples
javaandroidfirebasegoogle-cloud-firestorefirebaseui

Is there a way to remove a view from your holder class layout that you passed to the FireBasUI RecyclerAdapter?


Look so here is my problem: I have used this FireBaseRecyclerViewAdapter so I can upload and show my posts in my Homefragment. But here comes the problem: I have a layout where it shows a picture a and a text above. But I want to be able to show also posts without an Image so I declared a variable to every post called posttype if posttype is 0 it is a textpost otherwise it is a imagepost. The problem is how can I remove the imageview from the holder completly ?

I´ve tried:

        if(holder.textViewpostType.getText().equals("0")){
            holder.ImageViewpicture.setImageDrawable(null);
        }

But the problem is that the ImageView still stays there and is just invisble but I want it be completly gone you know ?

Here is the code for the entire PostsAdapter class a quick answer would be very helpful thx:

package com.LiFit;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class PostsAdapter extends FirestoreRecyclerAdapter<Posts, PostsAdapter.PostsHolder> {

    public PostsAdapter(@NonNull FirestoreRecyclerOptions<Posts> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull PostsHolder holder, int position, @NonNull Posts model) {
        holder.textViewdescription.setText(model.getDescription());
        holder.textViewpostType.setText(String.valueOf(model.getPostType()));
        holder.textViewtime.setText("uploaded at "+model.getTime());
        holder.textViewuid.setText(model.getUid());
        holder.textViewusername.setText(model.getUsername());
        holder.buttonlikes.setText("Likes: "+model.getLikes());
        Picasso.get().load(model.getProfileImage()).into(holder.circleImageViewprofileImage);
        Picasso.get().load(model.getPicture()).into(holder.ImageViewpicture);
        holder.textViewuid.setVisibility(View.INVISIBLE);
        holder.textViewpostType.setVisibility(View.INVISIBLE);
        if(holder.textViewpostType.getText().equals("0")){
            holder.ImageViewpicture.setImageDrawable(null);

        }
    }

    @NonNull
    @Override
    public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts_layout, parent, false);
        return new PostsHolder(v);
    }

    class PostsHolder extends RecyclerView.ViewHolder {
        TextView textViewdescription, textViewpostType, textViewtime, textViewuid, textViewusername;
        Button buttonlikes;
        ImageView ImageViewpicture;
        CircleImageView circleImageViewprofileImage;

        public PostsHolder(@NonNull View itemView) {
            super(itemView);
            textViewdescription = itemView.findViewById(R.id.post_description);
            textViewpostType = itemView.findViewById(R.id.post_postType);
            textViewtime = itemView.findViewById(R.id.post_uploadTime);
            textViewuid = itemView.findViewById(R.id.post_userUID);
            textViewusername = itemView.findViewById(R.id.post_userName);
            buttonlikes = itemView.findViewById(R.id.post_likes);
            ImageViewpicture = itemView.findViewById(R.id.post_image);
            circleImageViewprofileImage = itemView.findViewById(R.id.post_profileImage);
        }
    }
}

Solution

  • Bro I am so dumb

    just do this if you are wondering: holder.ImageViewpicture.setVisibility(View.GONE);