RecyclerView onClick gives wrong item

recyclerview item click listener in activity android
recyclerview onclick position
recyclerview item click listener in fragment
how to open a different fragment on recyclerview item onclick
recyclerview item click in fragment
how to open a different activity on recyclerview item onclick example
recyclerview select item
get adapter position in activity

I have a RecyclerView that shows a list of student details. The issue I face is when I click on the first item it returns the data of the second item. I believe that it has something to do with getAdapterPosition(), but I'm not sure.

In this case, it returns the Roll No. of the second student(item) when the first student is selected.

Any help is appreciated


RecyclerViewAdapter

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
    Context context;
    public List<StudentDataModel> dataModels;
    public StudentDataModel dataAdapter;

    public StudentAdapter(List<StudentDataModel> getDataAdapter, Context context){
        super();
        this.dataModels = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
        dataAdapter =  dataModels.get(position);        
        viewHolder.NameStudentCard.setText(dataAdapter.getName());
        viewHolder.RankStudentCard.setText(dataAdapter.getRank());
        viewHolder.RollStudentCard.setText(dataAdapter.getRoll());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyDataSetChanged();                
                Toast.makeText(v.getContext(), "Tapped on" + dataAdapter.getRoll(), Toast.LENGTH_SHORT).show(); //Here it returns the Roll No. of the second student(item) in the recyclerView.                
                Intent intent = new Intent (v.getContext(), StudentDetailsActivity.class);                
                v.getContext().startActivity(intent);
            }
        });

    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public TextView NameStudentCard;
        public TextView RankStudentCard;
        public TextView RollStudentCard;

        public ViewHolder(View itemView) {
            super(itemView);
            NameStudentCard=(TextView)itemView.findViewById(R.id.textViewName);
            RankStudentCard=(TextView)itemView.findViewById(R.id.textViewRank);
            RollStudentCard =(TextView)itemView.findViewById(R.id.textViewEmployeeID);

        }
    }

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

}

Yes, because you are declaring object as global object. So it is getting update with last visible row's object, because onBindViewHolder() gets called for each visible row sequentially. So it will work properly only for last visible row.

So you need to declare it inside method :

comment this :

   // public StudentDataModel dataAdapter;

declare it here :

@Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {

   final StudentDataModel dataAdapter =  dataModels.get(position); // add this here

}

RecyclerView Item OnClick returns Wrong item · Issue #1164 , I'm showing Missions in recycler view. Each Mission is of Class Mission. I'm using Mission Adapter and Mission View Holder in order to show  Hello Friends, Today I am going share how to use RecyclerView with click effect & click event handling on item in android. I know there is lot of tutorial available to do this but still I am sharing this because, every time I work with RecyclerView, I have to google for click effect and click event on recyclerview item each time.

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {

Context context;

public List<StudentDataModel> dataModels;


public StudentAdapter(List<StudentDataModel> getDataAdapter, Context context){

super();

this.dataModels = getDataAdapter;
this.context = context;
}

public StudentDataModel dataAdapter;


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;

}

@Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {

     dataAdapter =  dataModels.get(position);

    viewHolder.NameStudentCard.setText(dataAdapter.getName());

    viewHolder.RankStudentCard.setText(dataAdapter.getRank());

    viewHolder.RollStudentCard.setText(dataAdapter.getRoll());




}




@Override
public int getItemCount() {

    return dataModels.size();
}


class ViewHolder extends RecyclerView.ViewHolder{

    public TextView NameStudentCard;
    public TextView RankStudentCard;
    public TextView RollStudentCard;

    public ViewHolder(View itemView) {

        super(itemView);

        NameStudentCard=(TextView)itemView.findViewById(R.id.textViewName);
        RankStudentCard=(TextView)itemView.findViewById(R.id.textViewRank);
        RollStudentCard =(TextView)itemView.findViewById(R.id.textViewEmployeeID);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(v.getContext(), "Tapped on" + dataModels.get(getAdapterPosition()).getRoll(), Toast.LENGTH_SHORT).show(); 

                Intent intent = new Intent (v.getContext(), StudentDetailsActivity.class);

                v.getContext().startActivity(intent);
            }
        });

    }
}

}

RecyclerView Item Click Listener the Right Way, RecyclerView Item Click Listener the Right Way Just the above line of code gives us access to a setter and getter Item Click the bad way. RecyclerView item onClick. RecyclerView does not have an OnItemClickListener like it's predecessor, ListView.However, detecting item clicks is pretty simple. Set an OnClickListener in your ViewHolder creation:

Try this code.. make interface into adapter class for click handling like this way..

 onItemClickListner onItemClickListner;

public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

 public interface onItemClickListner{
    void onItemClick(String data); // pass any data;
}


@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    // below code handle click event on recycler view item.
    String data=mStringList.get(position);
    holder.textView.setText(data);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onItemClick(data); //pass your data;
        }
    });
}

after that bind adapter into recyclerview and adapter not null then called below code..

    recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
        @Override
        public void onItemClick(String data) {
            // perform your operation.
            recyclerViewAdpater.notifyDataSetChanged();
        }
    });

Better Way to Get The Item Position in Android's RecyclerView, Most of the available applications today always have a screen where they show us a list of items. The main screen of WhatsApp provides us  If you’ve used a RecyclerView, you may know that they don’t have a setOnItemClickListener as ListView had, so we have to create our own way to do it. Many people asked me how to do this, so that’s why I decided to write about it.

How to open a different fragment on recyclerview item onclick, How to open a different fragment on recyclerview item onclick. Otherwise I find performance to be quite bad when using the OnItemTouchListener - there's a I give my solution here: // define one interface 'RecyclerViewClickListener' for  RecyclerView Part 2: Android RecyclerView Adapter. This is the second post in our Android RecyclerView series. In previous post, you have find a very basic example about the RecyclerView in Android development.

Android: On item click listener for RecyclerView Adapter, Adapter , unlike its ListView colleague, does not have a item click listener. OnClickListener and then use indexOfChild to get the position of the view in the recycler may have the incorrect position, if the internal position of the views change. Previously, a ListView’s or GridView’s onItemClickListener managed item clicks. A RecyclerView doesn’t provide methods like this because its focus is ensuring the position and management of the items within. The job of listening for actions is now the responsibility of the RecyclerView item and its children.

Getting your clicks on RecyclerView, Unfortunately RecyclerView is missing a couple of features that ListView had built​-in. RecyclerView allows you to set an OnClickListener in your adapter, to the ViewHolder, is complicated for catching a simple item click. As you know RecyclerView doesn’t have item click listener. In this tutorial, we will learn how to handle item clicks on RecyclerView. We will keep going from previous tutorial – Making Album List 2. So if you don’t have source code of previous tutorial, download it from here. We will write all codes on it.

Comments
  • Why you are notifying data change onClick event?? Remove notifyDataSetChanged(); and check.
  • why are you calling notifyDataSetChanged()? Does the first element display the right data in the recyclerview? I see that you're setting the roll in the textview of the card, is that working failing too? or is it just on the click listener?
  • first of all change the arg of onBindViewHolder final int position to int postition