简体   繁体   中英

How to enable the adapter in the mainactivity for RecyclerView?

I completely copied a simple example to study the cursor adapter for the RecyclerView, but I do not understand how to activate it in the main activity. I used this example from GitHub CursorRecyclerViewAdapter

I think something like this

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

But it does not work and displays a list without the data. Maybe someone can tell me what I'm doing wrong

to get data from the table I wrote inside class MyListItem

    public static MyListItem fromCursor(Cursor cursor)
{
    MyListItem listitem = new MyListItem();
    listitem.setName(cursor.getString(cursor.getColumnIndex(DataBase.COLUMN_LITERS)));
    return listitem;
}

all the rest exactly as in the example (of course I changed the references to my database)

upd. My MainActivity

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

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

I think you missed layoutManager

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM