Recyclerview + Content provider + CursorLoader

Abdul Rahman picture Abdul Rahman · Jan 1, 2015 · Viewed 9.2k times · Source

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?

Answer

luckyhandler picture luckyhandler · Nov 22, 2015

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