制作RecyclerView
Adapter class
并且不知道为什么getItem()
方法丢失。
我的适配器类:
package com.example.pc.example;
import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .MyHolder> {
private Context mContext;
private Cursor mCursor;
public MyRecyclerAdapter (Cursor mCursor) {
this.mCursor = mCursor;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
mContext = parent.getContext();
View itemView = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
//here all my life i ussed the getItem() method, but now it is missing
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView tv1, tv2;
public MyHolder (View itemView) {
super(itemView);
tv1= (TextView) itemView.findViewById(R.id.tv1);
tv2= (TextView) itemView.findViewById(R.id.tv2);
}
@Override
public void onClick(View v) {
}
}
}
在ScreenShot中,您可以看到有getItemCount()
,getItemId(position);
和getItemViewType(position);
,但没有getItem(position)
。
有人可以解释我发生了什么吗? 非常感谢提前!
答案 0 :(得分:0)
缺少这个:
public Model getItem(int position) {
if (position < 0 || position >= getItemCount()) {
throw new IllegalArgumentException("Item position is out of adapter's range");
} else if (mCursor.moveToPosition(position)) {
return new Model (mCursor);
}
return null;
}
答案 1 :(得分:0)
BaseAdapter
并且通常与Adapter
一起使用的 ListView
有一个名为getItem(int position)
的抽象方法,应该在具体的Adapter类中实现。
abstract getItem(int position)
RecyclerView.Adapter
没有abstract getItem(int)
方法。所以,你不能覆盖它......
RecyclerView.Adapter class.. There's no getItem(int) method
如果您需要在特定位置检索项目,您可以自己创建方法。只需添加您在其他答案中提到的代码:
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .MyHolder> {
public Model getItem(int position) {
if (position < 0 || position >= getItemCount()) {
throw new IllegalArgumentException("Item position is out of adapter's range");
} else if (mCursor.moveToPosition(position)) {
return new Model (mCursor);
}
return null;
}
}