skip to Main Content

i have a recyclerview for a movie and i need the movie name for using intent putextra. I Already make the click listener and got the adapter position, but but it only returns the array of the adapter like in the picture.

picture of output in logcat

how can i get the strings so i can use the intent putextra?

here is my recyclerview code

public class movieRecyclerViewAdapter extends RecyclerView.Adapter<movieRecyclerViewAdapter.myViewHolder> {

private static final String TAG = "movieRecyclerViewAdapter";
private onMovieClickListener movieClickListener;
private final ArrayList<movie> movies;

public movieRecyclerViewAdapter(ArrayList<movie> movieArrayList, onMovieClickListener onMovieClickListener){
    this.movies = movieArrayList;
    this.movieClickListener = onMovieClickListener;
    Log.d("mainMenuAdmin", "Adapter List size : " + movieArrayList.size() );
}

@NonNull
@Override
public movieRecyclerViewAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_movie, parent, false);
    return new myViewHolder(view, movieClickListener);
}

@Override
public void onBindViewHolder(@NonNull movieRecyclerViewAdapter.myViewHolder holder, int position) {
    Log.d(TAG, "onBindViewHolder: called");
    String img = movies.get(position).getMovie_image();
    holder.judul.setText(movies.get(position).getNamaMovie());
    holder.tahun.setText(movies.get(position).getTahunMovie());
    Picasso.get().load(img).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round).into(holder.image);

}

@Override
public int getItemCount() {
    return movies.size();
}

public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private final TextView judul, tahun;
    private final ImageView image;
    RelativeLayout parent;
    onMovieClickListener onMovieClickListener;

    public myViewHolder(@NonNull View itemView, onMovieClickListener onMovieClickListener) {
        super(itemView);
        judul = itemView.findViewById(R.id.txtJudul);
        tahun = itemView.findViewById(R.id.txtTahun);
        image = itemView.findViewById(R.id.imagePoster);
        parent = itemView.findViewById(R.id.parent_layout);
        this.onMovieClickListener = onMovieClickListener;

        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        onMovieClickListener.onMovieClick(view, this.getLayoutPosition());
    }
}

public interface onMovieClickListener{
    void onMovieClick(View v, int position);

}}

i’ve tried everything but it still return the array value not the string value of movie name

2

Answers


  1. You have made a good effort 👌 but you are doing few things wrong here. Let me explain you all things step by step.

    1. You need a string value from your click listener but you are passing a view + position in click. so to fix this you firstly need to change you interface like this.
        public interface onMovieClickListener{
            void onMovieClick(movie movie, int position);
        }
    
    1. 2nd mistake is that you don’t have your movie in your ViewHolder. Thats why you can not pass string in your listener. To fix this you need to change your myViewHolder like below.
        public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            private final TextView judul, tahun;
            private final ImageView image;
            RelativeLayout parent;
            movie movie;
            onMovieClickListener onMovieClickListener;
    
            public myViewHolder(@NonNull View itemView) {
                super(itemView);
                judul = itemView.findViewById(R.id.txtJudul);
                tahun = itemView.findViewById(R.id.txtTahun);
                image = itemView.findViewById(R.id.imagePoster);
                parent = itemView.findViewById(R.id.parent_layout);
                itemView.setOnClickListener(this);
    
            }
            
    
            public void bind(movie movie, onMovieClickListener onMovieClickListener)
            {
                this.movie = movie;
                this.onMovieClickListener = onMovieClickListener;
                
            }
    
            @Override
            public void onClick(View view) {
                onMovieClickListener.onMovieClick(movie, this.getLayoutPosition());
            }
        }
    
    1. Change your adapter functions like this.
        @NonNull
        @Override
        public movieRecyclerViewAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_movie, parent, false);
            return new myViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull movieRecyclerViewAdapter.myViewHolder holder, int position) {
            Log.d(TAG, "onBindViewHolder: called");
            String img = movies.get(position).getMovie_image();
            holder.judul.setText(movies.get(position).getNamaMovie());
            holder.tahun.setText(movies.get(position).getTahunMovie());
            Picasso.get().load(img).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round).into(holder.image);
    
            holder.bind(movies.get(position), this.movieClickListener);
        }
    

    That’s IT!!!

    Now you have your movie in your listener and you can use your movie to get any variable inside movie like movie name etc..

    I am pasting whole java file here so that you can just copy and paste code to test.

    public class movieRecyclerViewAdapter extends RecyclerView.Adapter<movieRecyclerViewAdapter.myViewHolder> {
    
    private static final String TAG = "movieRecyclerViewAdapter";
    private onMovieClickListener movieClickListener;
    private final ArrayList<movie> movies;
    
    public movieRecyclerViewAdapter(ArrayList<movie> movieArrayList, onMovieClickListener onMovieClickListener){
        this.movies = movieArrayList;
        this.movieClickListener = onMovieClickListener;
        Log.d("mainMenuAdmin", "Adapter List size : " + movieArrayList.size() );
    }
    
    @NonNull
    @Override
    public movieRecyclerViewAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_movie, parent, false);
        return new myViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull movieRecyclerViewAdapter.myViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: called");
        String img = movies.get(position).getMovie_image();
        holder.judul.setText(movies.get(position).getNamaMovie());
        holder.tahun.setText(movies.get(position).getTahunMovie());
        Picasso.get().load(img).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher_round).into(holder.image);
    
        holder.bind(movies.get(position), this.movieClickListener);
    }
    
    @Override
    public int getItemCount() {
        return movies.size();
    }
    
    public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private final TextView judul, tahun;
        private final ImageView image;
        RelativeLayout parent;
        movie movie;
        onMovieClickListener onMovieClickListener;
    
        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            judul = itemView.findViewById(R.id.txtJudul);
            tahun = itemView.findViewById(R.id.txtTahun);
            image = itemView.findViewById(R.id.imagePoster);
            parent = itemView.findViewById(R.id.parent_layout);
            itemView.setOnClickListener(this);
    
        }
    
    
        public void bind(movie movie, onMovieClickListener onMovieClickListener)
        {
            this.movie = movie;
            this.onMovieClickListener = onMovieClickListener;
    
        }
    
        @Override
        public void onClick(View view) {
            onMovieClickListener.onMovieClick(movie, this.getLayoutPosition());
        }
    }
    
    public interface onMovieClickListener{
        void onMovieClick(movie movie, int position);
    
    }}
    

    **

    Few suggestions:

    1. Don’t send View via your listener to your activity/fragment
    2. Your camel case for your variables.
    3. Start class name with capital letter.

    **
    These few suggestions will make your code more readable.

    Happy Coding 💚!!

    Login or Signup to reply.
  2. Change interface listener to :

    public interface onMovieClickListener {
    void onMovieClick(View v, Movie movie);
    }
    

    Then define your setOnClickListener as :

    itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {                   
    
                onMovieClickListener.onMovieClick(v, movies.get(getAdapterPosition()));
                    }
                }
            });
    

    You’ll be able to get the selected movie and display name as needed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search