无法解析方法';get(java.lang.String)和#x27;

无法解析方法';get(java.lang.String)和#x27;,java,android,Java,Android,如何在recyclerView中获取行的位置并解决get问题 public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyViewHolder> { private String[] listOfItems; public MyListAdapter(String[] listOfItems){ this.listOfItems = listOfIt

如何在recyclerView中获取行的位置并解决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);
        }
    }




}
公共类MyListAdapter扩展了RecyclerView.Adapter{
私有字符串[]列表项;
公共MyListAdapter(字符串[]列表项){
this.listOfItems=listOfItems;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int i){
布尔attachViewImmediatelyToParent=false;
View singleItemLayout=LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,attachViewImmediatelyToParent);
MyViewHolder MyViewHolder=新的MyViewHolder(singleItemLayout);
返回myViewHolder;
}
@凌驾
公共无效onBindViewHolder(最终MyViewHolder,内部位置){
holder.textShow.setText(列表项[位置]);
holder.textShow.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
解决进餐吐司的问题
Toast.makeText(holder.textShow.getContext(),“您按下了“+listOfItems.get(holder.getLayoutPosition()+”项”)、Toast.LENGTH\u SHORT.show();
解决吃吐司的问题
}
});
}
@凌驾
public int getItemCount(){
返回listOfItems.length;
}
类MyViewHolder扩展了RecyclerView.ViewHolder{
文本视图文本显示;
公共MyViewHolder(查看项目视图){
超级(项目视图);
textShow=(TextView)itemView.findViewById(R.id.tvphrase);
}
}
}

ListofItem
在Java中是一个
数组
而不是
列表

你应使用:

listOfItems[holder.getLayoutPosition()];
您应该检查您试图在``处访问的项的索引是否超出范围:

if (holder.getLayoutPosition() < listOfItems.length) {
    listOfItems[holder.getLayoutPosition()];
}
else {
    Log.d("TAG", "Error: index out of bounds");
}
if(holder.getLayoutPosition()
要访问Java中的成员,请执行以下操作:

  • 对于:
    您的_数组[索引]
  • 对于a:
    您的列表。获取(索引)

最好

小心,有两个问题:

Toast.makeText(holder.textShow.getContext(),“您按下了“+listOfItems.get(holder.getLayoutPosition()+”项”),Toast.LENGTH_SHORT.show()

1) 输入错误:连接
+“项”
应该在结束括号
之后

2)
listOfItems
是数组,而不是列表,所以use应该使用
[]
语法

所以,正确的路线是正确的
Toast.makeText(holder.textShow.getContext(),“您按下了“+listOfItems[holder.getAdapterPosition()]”+项,Toast.LENGTH_SHORT).show()

更新p.S


同样,最好使用
getAdapterPosition()
而不是
getLayoutPosition()
内部侦听器

不客气。如果你的问题得到了答案,请将其标记为已解决。