2

How can I fetch position for row in recyclerView and solve the problem of get

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyViewHolder>  {




        private String[] listOfItems;

    public MyListAdapter(String[] listOfItems){
        this.listOfItems = listOfItems;

    }

    @Override
    public MyViewHolder onCreateViewHolder( ViewGroup parent, int i) {
        Boolean attachViewImmediatelyToParent = false;
        View singleItemLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,attachViewImmediatelyToParent);
        MyViewHolder myViewHolder = new MyViewHolder(singleItemLayout);


        return myViewHolder;
    }




    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.textShow.setText(listOfItems[position]);
        holder.textShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

Resolve the problem of get in Toast
                Toast.makeText(holder.textShow.getContext(), "you pressed the " + listOfItems.get(holder.getLayoutPosition()+" item"), Toast.LENGTH_SHORT).show();
Resolve the problem of getting in Toast
            }
        });



    }


    @Override
    public int getItemCount() {
        return  listOfItems.length;
    }




    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textShow;
        public MyViewHolder(View itemView) {
            super(itemView);
            textShow = (TextView) itemView.findViewById(R.id.tvphrase);
        }
    }




}

enter image description here

2 Answers 2

1

ListofItem is an Array not a List in Java.

You should use:

listOfItems[holder.getLayoutPosition()];

And you should check if the index of the item you are trying to access at `` is not out of bounds:

if (holder.getLayoutPosition() < listOfItems.length) {
    listOfItems[holder.getLayoutPosition()];
}
else {
    Log.d("TAG", "Error: index out of bounds");
}

To access members in Java:

  • For an Array : your_array[index]
  • For a List : your_list.get(index)

Best

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. If that answer to your question, mark it as solved.
1

Be careful, there are two problems:

Toast.makeText(holder.textShow.getContext(), "you pressed the " + listOfItems.get(holder.getLayoutPosition()+" item"), Toast.LENGTH_SHORT).show();

1) You have typo error: concatenation + "item" should be after closing bracket )

2) listOfItems is array, not list, so use should use [] syntax.

So, proper line is Toast.makeText(holder.textShow.getContext(), "you pressed the " + listOfItems[holder.getAdapterPosition()]+" item", Toast.LENGTH_SHORT).show();

UPDATE P.S.

As well, it's better use getAdapterPosition() and not getLayoutPosition() inside listeners

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.