18

I didn't google out a solution till now to replace listview in my project, because I need to use the cursor linked with the sqlite.

Old way as followed: listview.setAdapter(cursorAdapter) in this way, I can get the cursor to deal with data in database

but now, recycleview.setAdapter(recycleview.adapter) it doesn't recognize the adapter extending BaseAdapter

so anyone can give me a hand?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
machinezhou
  • 679
  • 2
  • 6
  • 16
  • 1
    See my answer here: http://stackoverflow.com/a/27732748/1371730 Nice trick :) – nbtk Jan 01 '15 at 17:01
  • Possible duplicate of [Using the recyclerview with a database](http://stackoverflow.com/questions/26517855/using-the-recyclerview-with-a-database) – e4c5 Oct 15 '15 at 06:27

2 Answers2

31

Implementing it yourself is actually quite simple:

public class CursorAdapter extends RecyclerView.Adapter<ViewHolder>{

    Cursor dataCursor;

    @Override
    public int getItemCount() {
        return (dataCursor == null) ? 0 : dataCursor.getCount();
    }


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

     public Cursor swapCursor(Cursor cursor) {
        if (dataCursor == cursor) {
          return null;
        }
        Cursor oldCursor = dataCursor;
        this.dataCursor = cursor;
        if (cursor != null) {
          this.notifyDataSetChanged();
        }
        return oldCursor;
      }

    private Object getItem(int position) {
        dataCursor.moveToPosition(position);
        // Load data from dataCursor and return it...
      }

}
Nyx
  • 2,233
  • 1
  • 12
  • 25
  • 1
    eh, it's not what I mean buddy, I indeed found a project on github later in which cursorAdapter is implemented by the developer himself.But it's not that easy and I'm not sure if it will come out a bug or something unexpected. So due to my own ability and stability concern, I'd rather wait for the official release. – machinezhou Oct 31 '14 at 04:25
  • 2
    @machinezhou I don't think they are going to add an official release for it, so far this has been working flawlessly for me – Nyx Jan 12 '15 at 12:13
  • The feeling I get is that there isn't going to be an official implementation owing to the "no DB work on the UI thread" vibe they've been pushing since CursorLoader. Anyways, as to the solution here, going on other implementations I've seen, you might want to check for potential errors caused by moving the cursor or cursor state. The implementations I've seen so far just throw exceptions in this case, which seems ungraceful to me. – Tarkenfire Jan 21 '15 at 12:50
  • I'm surprised, but this simple solution worked flawlessly for me. – PPartisan Mar 06 '15 at 12:04
  • tried and onBindViewHolder is called as I get logs but nothing is displayed – ingsaurabh Jul 04 '15 at 07:51
  • @ingsaurabh If onBindViewHolder is being called then the adapter does have some data backing it. Perhaps your method of pulling data from the cursor isn't functioning as expected? If you're still having issues post up a separate question and I can give you a hand. – Nyx Jul 08 '15 at 19:05
  • @nyx thanks I solved the problem, issue is not with adapter but some dumb mistake I had done in my code – ingsaurabh Jul 09 '15 at 06:51
7

The new RecyclerView works with a new RecyclerView.Adapter base class. So it doesn't work with the CursorAdapter.

Currently there is no default implementation of RecyclerView.Adapter available.

May be with the official release, Google will add it.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841