A List With Header - how to use with cursors?

from the CommonsWare Community archives

At May 2, 2018, 4:59am, Shahood asked:

Hi Mark,

First of all, I must appreciate the effort u put in drafting and explaining this subsection in chapter RecyclerView. I really enjoyed the pattern applied in getItem() method, which at first boggled my mind to the level of quitting this topic. That was due to my keen interest in learning how to implement lists with headers and the confidence in your command on Android development in general, that I continued digging into it and finally (after implementing log statements at some 5 points in the said method) understood it.
I took it even further by getting data from a cursor, sorting it and putting it into classes and then pulling it from there to use in getItem() method. After a lot of effort, it finally worked.
But there are some areas that I need u to comment on:

  1. the pattern/code mentioned in getItem(), i believe it cant be a generic pattern and we might have to turn it upside down as the need arises. I also had to change it but i must admit that i didn’t change the soul of it, i just changed it a tad bit. I actually tried to design my data (obtained from the cursor) to fit into that pattern. In your view, is it a generic pattern and the data should be designed according to that?
  2. Is it possible to get headers in a list by directly fetching the data from the cursor and not first custom designing the data by sorting it, putting it into classes and/or arrays and then using it? For instance, lets say i have 02 tables in my database: 1) for item categories 2) for items where each item has to have an item category assigned to it. Now i want my items to be presented in a list BUT with item categories as list headers. Can the getItem() method pull the relevant item right from the cursor data obtained from the items table?
  3. Is there a way, we can make these headers “sticky”? Thats for the lists where a lot of items are defined under a header and we want the header to stay on top until the next (or previous in case of reverse scroll) header replaces it.

I have more questions relating to it but I wont throw these at least now. I have a feeling these will get answered when i go through the other trails. If not, pl expect more questions in future!

Thanking in anticipation!

Shahood


At May 2, 2018, 10:51am, mmurphy replied:

In your view, is it a generic pattern and the data should be designed according to that?

No offense, but I do not know what you are referring to.

Is it possible to get headers in a list by directly fetching the data from the cursor and not first custom designing the data by sorting it, putting it into classes and/or arrays and then using it?

I presume that would be possible. I have not tried it.

Can the getItem() method pull the relevant item right from the cursor data obtained from the items table?

I presume that would be possible. I have not tried it.

Is there a way, we can make these headers “sticky”?

There are plenty of libraries for that on the Android Arsenal. I have not used any of them and so cannot comment on them, beyond that they exist.


At May 4, 2018, 3:25am, Shahood replied:

Thanks, I’ll try to give a shot to these libraries.