[英]Cannot resolve method 'get(java.lang.String)'
How can I fetch position for row in recyclerView and solve the problem of get如何在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);
}
}
}
ListofItem
is an Array
not a List
in Java. ListofItem
是一个Array
而不是 Java 中的List
。
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:在 Java 中访问成员:
your_array[index]
your_array[index]
your_list.get(index)
your_list.get(index)
Best最好的事物
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 )
1)您有错字错误:串联
+ "item"
应该是闭架后)
2) listOfItems
is array, not list, so use should use []
syntax. 2)
listOfItems
是数组,不是列表,所以使用应该使用[]
语法。
So, proper line is Toast.makeText(holder.textShow.getContext(), "you pressed the " + listOfItems[holder.getAdapterPosition()]+" item", Toast.LENGTH_SHORT).show();
所以,正确的行是
Toast.makeText(holder.textShow.getContext(), "you pressed the " + listOfItems[holder.getAdapterPosition()]+" item", Toast.LENGTH_SHORT).show();
UPDATE PS更新PS
As well, it's better use getAdapterPosition()
and not getLayoutPosition()
inside listeners同样,最好在侦听器中使用
getAdapterPosition()
而不是getLayoutPosition()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.