ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2

You can read Part 1 here.

Next, step of the recyclerView implementation is to implement the recyclerView Adapter. It has two main jobs:

  1. to create the item views and return them to the recyclerView
  2. and to populate the item views.

RecyclerView creates the items that are visible but also for some before and after. Instead of creating new items, it takes one item that is last/old, moves it to the front and then recyclerView populates it with new data. In other words, the adapter recycles the views/items

Create a recyclerView Adapter

Create a new class file for the adapter.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Create an inner class ViewHolder that extends RecyclerView.ViewHolder. ViewHolder holds a reference to the main view and to the contained views. It must have fields for all the views used to display the information we need. Create also a default constructor.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Now extend the adapter class, the outer class. It should extend RecyclerView.Adapter that takes a type argument the ViewHolder you created.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Now, override the three methods that are important.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Before we implement the methods, we need three private fields. Context, Cursor and a List for the data

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Now while on the Context or Cursor, press Alt + Enter and click Add constructor parameters. Select Context, List and click OK. It creates a new constructor with those two parameters. Now create a LayoutInflater from the context.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

You need to find the column position. First create private integer fields and then use getColumnIndex to get the column position.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ
, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Create a View and return the ViewHolder. Inflate takes 3 parameters, the item layout, the viewgroup that passed, and false because we do not want the view to be attached to its parent.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

getIemCount must return the total amount of the data we have .

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Before we bind the data to our view we need to get the references for all the views in the item layout. Create public fields so they can be called from the outer class and then get the references in the ViewHolder inner class.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Now, it is time to bind the data to the view. First, move the cursor to the position i, the 2nd argument, then get the values from the cursor and set the text to the views

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Now at the activity java class that has the recyclerView, we need to create a reference to the recyclerView

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Create a new Linear Layout Manager and set it to the recyclerView

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Associate the RecyclerView adapter with the recyclerView. But first, we need to instantiate the adapter with the data.

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

Here is how it looks 

, <span>ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2</span>, KandZ

END OF PART 2

98850cookie-checkANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2ANDROID – RECYCLERVIEW ADAPTER with Cursor PART 2

Related posts