20

I have some data in SQLite database.I have a content provider which will get the data from the database. Now the problem is how do i implement cursorLoader to work in hand with recyclerview?

Also, anyone can explain why we need to transfer the data from cursor to an object to display in a listview/recyclerview and not straight from a cursor?

For example, In the custom cursorAdapter class,

Person person = new Person(cursor.getString(cursor.getColumnIndexOrThrow(PERSON_NAME)));
textview.setText = person.getName();

OR

textview.setText = cursor.getString(cursor.getColumnIndexOrThrow(PERSON_NAME));

Which one of the above method is better?

In the past, we used to have listview and gridview and it seems now they are combined to become recyclerview. Then, how do i implement a grid based recyclerview?

Abdul Rahman
  • 1,653
  • 1
  • 19
  • 20
  • you dont have to use any list: use ItemBridgeAdapter + CursorObjectAdapter or bind the Cursor directly in your custom RecyclerView.Adapter – pskink Jan 01 '15 at 12:58
  • honestly i dont know why CursorObjectAdapter is in "leanback" support library which is designed for TV devices, imho it should be part of any "general purposes" support library – pskink Jan 01 '15 at 14:21
  • @pskink I am having to solve this problem presently. Will you please provide a short example of how I could `bind the Cursor directly in [my] custom RecyclerView.Adapter`? That would be tremendously helpful. Thanks. And please tag me in the comment of the response when you do so I know. Thanks for any help you can provide. – Katedral Pillon Sep 05 '15 at 04:57
  • 1
    @KatedralPillon use this for example: https://gist.github.com/Shywim/127f207e7248fe48400b – pskink Sep 05 '15 at 05:12
  • @pskink Thanks. Is it more efficient than loading the cursor into an ArrayList and then passing the ArrayList to the RecyclerView? – Katedral Pillon Sep 05 '15 at 05:48
  • 1
    @KatedralPillon of course! never load the cursor into any list, it seems that 99% folks here do such a stupid thing i don't know why (they need a custom adapter (for a `ListView` it's a custom `BaseAdapter` / `ArrayAdapter`) / they need a POJO to keep data / they need a loop to iterate over the cursor) why??? i don't know – pskink Sep 05 '15 at 05:58
  • @pskink I am wondering if you have a more detailed example of how to set the cursor directly in a custom recyclerview. The link you posted alone is kind of confusing to a noob. – Micro Sep 11 '15 at 20:01
  • @MicroR extend `CursorRecyclerAdapter` and implement its `onBindViewHolderCursor` method, that's all – pskink Sep 11 '15 at 20:16

3 Answers3

4

In general you should try to separate view and data responsibilities. So what you need is to get all your objects from the database in advance then set up an Adapter which looks like the following:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private final List<Person> objectList = new ArrayList<>();

    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new ViewHolder(layoutInflater.inflate(R.layout.adapter_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final CustomAdapter.ViewHolder holder, final int position) {
        holder.bindItem(objectList.get(position));
    }

    // Set the persons for your adapter
    public void setItems(final List<Person> persons) {
        objectList.addAll(persons);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return objectList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView mTextViewTitle;
        private Object mObject;

        public ViewHolder(final View itemView) {
            super(itemView);
            mTextViewTitle = (TextView) itemView.findViewById(R.id.view_item_textViewTitle);                
            mTextViewTitle.setText(mObject.getText());
        }

        private void bindItem(@NonNull final Person object) {
            mObject = object;
        }
    }
}

Then you can bind the adapter to the RecyclerView by:

final CustomAdapter adapter = new CustomAdapter();
adapter.setItems(mPersons);
mRecyclerView.setAdapter();

To answer your second question ("In the past, we used to have listview and gridview and it seems now they are combined to become recyclerview. Then, how do i implement a grid based recyclerview?"):

When binding a LayoutManager to the RecyclerView you can decide which one you take:

final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);

or

final GridLayoutManager layoutManager = new GridLayoutManager(this, COLUMN_SPAN_COUNT);
mRecyclerView.setLayoutManager(layoutManager);

There are several LayoutManagers. Find out more here.

UPDATE: You don't have to load all items in advance, just rename setItems to addItems and you are good to go

luckyhandler
  • 8,848
  • 2
  • 40
  • 53
  • 1
    You say:"So what you need is to get all your objects from the database in advance", but what if I have 12.000 object to fetch from database, and this is the main reason choose ContentProvider mechanism (to do lazy loading for me) ? – miroslavign Jan 18 '16 at 15:15
  • How are you suppoused to keep the array in sync with the database? – 0xcaff Jan 12 '17 at 04:00
3

There are several Github gists/projects like this and this which show such a use case.

While you would be using a Adapter which is custom-tailored to a cursor adapter,you would use a GridLayoutManager/LinearLayoutManager as usual for the same.

srv_sud
  • 593
  • 7
  • 26
Droidekas
  • 3,256
  • 2
  • 24
  • 38
1

I think you can use directly Custom CursorAdapter for RecyclerView so you do not have to convert Cursor to ArrayList:

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {

    // Because RecyclerView.Adapter in its current form doesn't natively 
    // support cursors, we wrap a CursorAdapter that will do all the job
    // for us.
    CursorAdapter mCursorAdapter;

    Activity mContext;
    Random rnd;

    public ProductListAdapter(AppCompatActivity context, Cursor c) {

        mContext = context;
        rnd = new Random();

        mCursorAdapter = new CursorAdapter(mContext, c, 0) {

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                // Inflate the view here
                LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
                return inflater.inflate(R.layout.row_product_layout_grid, parent, false);
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                String productName = cursor.getString(cursor.getColumnIndex(TProduct.PRODUCT_NAME));

                // Binding operations
                ((TextView) view.findViewById(R.id.sub_product_name_text_view)).setText(productName);



                int color = Color.argb(200, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

                String url = "http://dummyimage.com/300/" + color + "/ffffff&text=" + (cursor.getPosition() + 1);

                Picasso
                        .with(context)
                        .load(url)
                        .placeholder(R.mipmap.ic_launcher) // can also be a drawable
                        .into((ImageView) view.findViewById(R.id.sub_product_image_view));
            }
        };
    }

    public void reQuery(Cursor c) {
        if (mCursorAdapter != null) {
            mCursorAdapter.changeCursor(c);
            mCursorAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mCursorAdapter.getCount();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // Passing the binding operation to cursor loader
        mCursorAdapter.getCursor().moveToPosition(position); //EDITED: added this line as suggested in the comments below, thanks :)
        mCursorAdapter.bindView(holder.view, mContext, mCursorAdapter.getCursor());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Passing the inflater job to the cursor-adapter
        View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
        return new ViewHolder(v);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View view;
        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.product_row_card_view);
        }
    }
}

May it will be useful to you. :)

Pratik Butani
  • 51,868
  • 51
  • 228
  • 375
  • https://stackoverflow.com/questions/39825125/android-recyclerview-cursorloader-contentprovider-load-more – zhirzh Oct 04 '20 at 19:37