Constantin N.
Constantin N.

Reputation: 2839

RecyclerView onclick returning wrong item

I'm Using a RecyclerView that works well. Except that when I click on element 1 it returns element 3 when I scroll down. And when I scroll up and click on the element 4 it returns the element 3.

This is my adapter

public class StarRVAdapter extends RecyclerView.Adapter<StarRVViewHolder>{

private Context c;
private ArrayList<Questions> questionEntries;
Questions qe;
public StarRVAdapter(Context c, ArrayList<Questions> questionEntries,Questions qe) {
    this.c = c;
    this.qe = qe;
    this.questionEntries = questionEntries;
}
@Override
public StarRVViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(c).inflate(R.layout.actu_list_item,parent,false);
    return new StarRVViewHolder(v,c);
}
@Override
public void onBindViewHolder(StarRVViewHolder holder, int position) {
     qe = (Questions)this.getItem(position);
    holder.setDate(qe.getQuestion_date());
    holder.setContent(qe.getQuestion_content());
    holder.setImg(qe.getImgUrl());
    holder.setTitle(qe.getQuestion_title());
    holder.setAsker(qe.getQuestion_username());
    holder.setIsRecyclable(true);

//this is My OnclickListener

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GoToView();

        }
    });
}
@Override
public int getItemCount() {
    return questionEntries.size();
}
public Object getItem(int position) {
    return questionEntries.get(position);
}
private void GoToView() {
    Intent intent = new Intent(c,QuestionView.class);
    Bundle b = new Bundle();
    intent.putExtra("QuestionRef",qe.getTag_id());
    intent.putExtra("ContentRef",qe.getQuestion_content());
    intent.putExtra("TitleRef",qe.getQuestion_title());
    intent.putExtra("ImgRef",qe.getImgUrl());
    intent.putExtra("UsernameRef",qe.getQuestion_username());
    intent.putExtra("DateRef",qe.getQuestion_date());
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(c,intent, b);
//This is returning another item
}
}

Please help me

Upvotes: 2

Views: 1773

Answers (2)

mac229
mac229

Reputation: 4839

final Questions qe = (Questions)this.getItem(position);
holder.setDate(qe.getQuestion_date());
holder.setContent(qe.getQuestion_content());
holder.setImg(qe.getImgUrl());
holder.setTitle(qe.getQuestion_title());
holder.setAsker(qe.getQuestion_username());
holder.setIsRecyclable(true);

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        GoToView(qe);

    }
});

and change your

private void GoToView(Questions qe) {
    Intent intent = new Intent(c,QuestionView.class);
    Bundle b = new Bundle();
    intent.putExtra("QuestionRef",qe.getTag_id());
    intent.putExtra("ContentRef",qe.getQuestion_content());
    intent.putExtra("TitleRef",qe.getQuestion_title());
    intent.putExtra("ImgRef",qe.getImgUrl());
    intent.putExtra("UsernameRef",qe.getQuestion_username());
    intent.putExtra("DateRef",qe.getQuestion_date());
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(c,intent, b);
//This is returning another item
}

Upvotes: 1

Shaishav
Shaishav

Reputation: 5312

You are working with the wrong Questions object here. Allocate it in your click itself.

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override 
    public void onClick(View view) {
        GoToView(holder.getAdapterPosition()); // passing position

    } 
}); 

Change the signature of GoToView accordingly:

private void GoToView(int position) { 
    Questions qe = (Questions)this.getItem(position);
    // Cool stuff with qe
}

No need to keep it an instance variable

Upvotes: 2

Related Questions