Cannot resolve method 'get(java.lang.String)'

Abd Al Rahman Al-Mashagbeh :

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

Maxouille :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=150536&siteId=1