Usage of Android SimpleCursorAdapter and CursorLoader
G

2

5

I am new to Android and am trying to get my header round the SimpleCursorAdapter and CursorLoader classes. From my understanding, all of the examples that I have seen use these two classes to load data into a ListView in a background thread (to not block the UI).

I am OK with this and have used this general approach with my own list of items, however I now want to be able to click on an item in the list and get the full information for the item. Is it usual practice to use SimpleCursorAdapter and CursorLoader to retrieve the details for a single item? or are they just meant for lists?.

Thanks.

Gump answered 26/10, 2012 at 8:40 Comment(0)
C
8

They are not meant for lists only. You can - and should - use them in detail views (activities) as well.

I've sketched a possible way to do so on my blog: http://www.grokkingandroid.com/using-loaders-in-android/

Creodont answered 26/10, 2012 at 8:44 Comment(5)
Your blog is great. That's how I learned loaders. However, I couldn't find the info you alluded to about detail views. Can you elaborate? Right now, I am just hacking a single item view together with SimpleCursorAdapter, specifying 0 for layout (nothing) in the constructor, and calling bindView explicitly with a LinearLayout as the detail view. Any help much appreciated.Solfatara
Why use an Adapter for a detail view? Adapters are for lists of data and not for a single detail record. I'm referring to the code in the onLoadFinshed() section of my post. Here I read the only row returned by the query and set the view elements explictly.Creodont
Well, you mentioned one should do so in your answer. The benefit of an adapter is to bind data to a partial view, and have it automatically updated using the cursor loader. Why should they be just for lists? Most other view systems allow binding to lists and singular items. (E.g., MVC MVVM). Did you just mean that you should use cursor loaders for detail views?Solfatara
Yes, I referred only to Loaders. Granted: I should have been more specific. Android doesn't provide this binding by default - other than for a few specific cases (like ListViews). But there are libraries out there, that do that. Since I don't use them, I cannot say anything about them.Creodont
@WolframRittmeyer Please do you think you could take at look at this question goo.gl/mdBsGFMesozoic
F
3

Think of Adapters as a layer of abstraction between your data (Cursor) and whatever you attach that Adapter to (ListView for example). This way, you have a common interface between your data (Cursor, ArrayList, whatever) and the View you display that data on (ListView, TableView, etc.), this is helpful because if you later find that you want to access your data through an ArrayList rather than a Cursor, then you simply swap out the adapter with a different one and you're ready.

Now considering your question, Adapters give an abstract access to information, therefore you can "ask" it for what information is stored and where. You could attach an OnItemClickListener to your ListView and then access your data from there.

Flew answered 26/10, 2012 at 8:54 Comment(4)
Why would one want to swap one adapter for another here? Having one adapter should be sufficient.Janis
@Janis I'm not sure I understand your question. If you swapped adapters you would still only have one adapter.Flew
~"swap out the adapter with a different one". I am trying to get clarification on this statement. You mean changing the code at compile-time, or run-time?Janis
@Janis I think you mean when writing the source code, which is the case. When I attach an adapter to my ListView, I can choose that adapter to be, for example, a SimpleCursorAdapter (given that my data exists in a database). If suddenly I decide that my data should just be stored in a plain text-file, then I can write an adapter that interprets data from that text-file and swap it out with my SimpleCursorAdapter. Here's the smart thing, my ListView doesn't care which adapter I've used! To the ListView, each adapter performs the same way, because they conform to the adapter interface.Flew

© 2022 - 2024 — McMap. All rights reserved.