我阅读了有关RecyclerViews和Cursor适配器的this thread,并尝试使用第一个solution。我对OnViewHolder函数的实现感到困惑。我知道OnViewHolder可以通过(ViewHolder,int)或(ViewHolder,Cursor)调用,但我不知道如何在我的项目中使用它。
这是我原来的适配器
public class FeedAdapter extends CursorAdapter {
/*
Etiqueta de Depuración
*/
private static final String TAG = FeedAdapter.class.getSimpleName();
/**
* View holder para evitar multiples llamadas de findViewById()
*/
static class ViewHolder {
TextView titulo;
TextView descripcion;
int tituloI;
int descripcionI;
}
public FeedAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout, null, false);
ViewHolder vh = new ViewHolder();
// Almacenar referencias
vh.titulo = (TextView) view.findViewById(R.id.titulo);
vh.descripcion = (TextView) view.findViewById(R.id.descripcion);
// Setear indices
vh.tituloI = cursor.getColumnIndex(ScriptDatabase.ColumnEntradas.TITULO);
vh.descripcionI = cursor.getColumnIndex(ScriptDatabase.ColumnEntradas.DESCRIPCION);
view.setTag(vh);
return view;
}
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder vh = (ViewHolder) view.getTag();
// Setear el texto al titulo
vh.titulo.setText(cursor.getString(vh.tituloI));
// Obtener acceso a la descripción y su longitud
vh.descripcion.setText(descripcion);
}
}
我的RecyclerView新适配器。 OnBindViewHolder尚未完成,因为我不知道如何设置它。
public class RVAdapter extends CursorRecyclerViewAdapter<RVAdapter.ViewHolder>{
private Cursor mCursorAdapter;
public RVAdapter(Context context, Cursor cursor){
super(context,cursor);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView titulo;
TextView descripcion;
int tituloI;
int descripcionI;
public ViewHolder (View view){
super(view);
cv = (CardView) itemView.findViewById(R.id.cv);
titulo = (TextView) itemView.findViewById(R.id.titulo);
descripcion = (TextView) itemView.findViewById(R.id.descripcion);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
ViewHolder vh = new ViewHolder(itemView);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
mCursorAdapter.moveToPosition(position);
final ViewHolder vh = (ViewHolder) view.getTag();
}
}
我应该使用&#34; onBindViewHolder(ViewHolder viewHolder,Cursor cursor)&#34;在这种情况下?我怎么能让它有效?
编辑1
这是扩展我的RVAdapter的抽象类
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private Context mContext;
private Cursor mCursor;
private boolean mDataValid;
private int mRowIdColumn;
private DataSetObserver mDataSetObserver;
public CursorRecyclerViewAdapter(Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
mDataValid = cursor != null;
mRowIdColumn = mDataValid ? mCursor.getColumnIndex("_id") : -1;
mDataSetObserver = new NotifyingDataSetObserver();
if (mCursor != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
}
public Cursor getCursor() {
return mCursor;
}
@Override
public int getItemCount() {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
}
return 0;
}
@Override
public long getItemId(int position) {
if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIdColumn);
}
return 0;
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(true);
}
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor);
@Override
public void onBindViewHolder(VH viewHolder, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(viewHolder, mCursor);
}
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*/
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*/
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
final Cursor oldCursor = mCursor;
if (oldCursor != null && mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (mCursor != null) {
if (mDataSetObserver != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIdColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
notifyDataSetChanged();
} else {
mRowIdColumn = -1;
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
return oldCursor;
}
private class NotifyingDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
super.onChanged();
mDataValid = true;
notifyDataSetChanged();
}
@Override
public void onInvalidated() {
super.onInvalidated();
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
}
}
答案 0 :(得分:1)
您可以使用ViewHolder和位置访问您的观看次数和字段 下面:
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
StructName name = names.get(position);
viewHolder.txttitle.setText(name.title);
viewHolder.rootItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
比使用光标更容易 如果这有用,请让我知道。
答案 1 :(得分:1)
使用Cursor,因为它更有效。
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
mCursorAdapter.moveToPosition(position);
viewHolder.titulo.setText(mCursorAdapter.getString(mCursorAdapter.getColumnIndex(ScriptDatabase.ColumnEntradas.TITULO)));
viewHolder.descripcion.setText(mCursorAdapter.getString(mCursorAdapter.getColumnIndex(ScriptDatabase.ColumnEntradas.DESCRIPCION)));
}