如何在RecyclerView的mainactivity中启用适配器?

时间:2017-12-12 13:00:42

标签: java android android-recyclerview

我完全复制了一个简单的例子来研究RecyclerView的游标适配器,但我不明白如何在主活动中激活它。 我在GitHub CursorRecyclerViewAdapter

中使用了这个例子

我觉得这样的事情

 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
 MyListCursorAdapter adapter = new MyListCursorAdapter(this, cursor);
 recyclerView.setAdapter(adapter);

但它不起作用并显示没有数据的列表。 也许有人可以告诉我我做错了什么

从我在class MyListItem

中写的表中获取数据
    public static MyListItem fromCursor(Cursor cursor)
{
    MyListItem listitem = new MyListItem();
    listitem.setName(cursor.getString(cursor.getColumnIndex(DataBase.COLUMN_LITERS)));
    return listitem;
}

所有其余内容与示例完全相同(当然我更改了对数据库的引用)

UPD。我的主要活动

public class MainActivity extends AppCompatActivity
{
Cursor cursor;
DataBase db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DataBase(this);
    db.open();

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    MyListCursorAdapter adapter = new MyListCursorAdapter(this, cursor);
    recyclerView.setAdapter(adapter);
}
}

upd2 MyListItem

public class MyListItem
{
private String name;

public void setName(String name)
{
    this.name=name;
}
public String getName()
{
    return name;
}

public static MyListItem fromCursor(Cursor cursor)
{
    MyListItem listitem = new MyListItem();

      listitem.setName(cursor.getString(cursor.getColumnIndex(DataBase.COLUMN_LITERS))
);
    return listitem;
}
}

的CursorAdapter

public class MyListCursorAdapter extends 
CursorRecyclerViewAdapter<MyListCursorAdapter.ViewHolder>
{

public MyListCursorAdapter(Context context,Cursor cursor)
{
    super(context,cursor);
}

public static class ViewHolder extends RecyclerView.ViewHolder
{
    public TextView mTextView;
    public ViewHolder(View view) {
        super(view);
        mTextView = (TextView) view.findViewById(R.id.name);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);
    ViewHolder vh = new ViewHolder(itemView);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor)
{
    MyListItem myListItem = MyListItem.fromCursor(cursor);
    viewHolder.mTextView.setText(myListItem.getName());
}
}

和CursorRecyclerViewAdapter

    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);
}

public void changeCursor(Cursor cursor)
{
    Cursor old = swapCursor(cursor);
    if (old != null) {
        old.close();
    }
}

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
    }
}
}

1 个答案:

答案 0 :(得分:0)

我认为你错过了layoutManager

    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.list);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    MyListCursorAdapter adapter = new MyListCursorAdapter(this, cursor);
    recyclerView.setAdapter(adapter);