Is it possible to make CursorAdapter be set in recycleview, just like ListView?

前端 未结 2 1022
轻奢々
轻奢々 2021-02-01 21:01

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.

2条回答
  •  半阙折子戏
    2021-02-01 21:14

    Implementing it yourself is actually quite simple:

    public class CursorAdapter extends RecyclerView.Adapter{
    
        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...
          }
    
    }
    

提交回复
热议问题