Is there a way to complete cursor queries off the main thread that update specific fields in a List of objects? I have looked into CursorAdapter examples from the Google Development Library, however, most solutions simply update ListView rows with the queried information, not pre-existing objects.
Is my only solution to query the information in an implementation specific thread, or is there something similar to CursorAdapter for the process I am describing. Thanks in advance! All links appreciated
This is pretty simple in Android by using Loader. Please refer
http://developer.android.com/guide/components/loaders.html
Loader is ideal for this. If you find any issues for implementing this, tell me. I can help you with some samples
Related
In my app, I am using the RealmBaseAdapter with Android's Listview, and I'm having some issues with the general lagginess of the application. I was wondering if anyone had any general pointers as to how I can optimize my listview so that is does not appear laggy to the user. I tried routing most of my getView method through an AsyncTask, but, as much of my getView is the model interacting with the UI, pretty much everything had to be put on the onPostExecute thread. Any advice would be greatly appreciated.
Following the advice here is a great first step: http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Otherwise this blog post by Romain Guy, also goes in depth with the tools you can use in order to find the root cause: http://www.curious-creature.com/2012/12/01/android-performance-case-study/
Without your adapter code, it is hard to say anything more concrete.
I could not find a tutorial on how to use AsyncTaskLoader to load a cursor from a table in SQLite.
Can someone pint me to some documentation?
Thank you
You can go for AsyncTask if you find AsyncTaskLoader a bit difficult. It is much easy to implement. Check the above link.
You can see the answer given in this post. I would suggest to use the Loader framework (here is the tutorial from the developers page). You will have to implement a ContentProvider. If you really want to avoid implementing a ContentProvider, do what is mentioned on the first link.
Could you point out or recommend reading on pros of using Loader regarding network/sqlite requests?
The only implementation - CursorLoader says it uses a ContentResolver. ContentResolvers as I read are used in conjunction with ContentProviders, which purpose is to expose inner-application data. It seems a bit messed up to me since I only intend to load data for internal usage. I cant see any point/benefits of using Loaders - what's bad with using ORM tool like greenDao in conjunction with plain AsyncTasks?
Are there design benefits? easy DI?
The CursorLoader is a very convinient method for querying because it takes care of the thread in an asyncronously manner. There is a lot of info here about it, you just have to search it. Here are two links that might help you to understand them.
Check inside Stackoverflow and full explanation on an expert blog.
Good luck!
I write an Android app and asking myself how to manage cursors. I know how to get data through a cursor back from the database. I don’t want to handle the lifecycle of these cursors by myself.
For Android 2.x and below I used, according to the Android API, methods like managedQuery and startManagingCursor. These methods are now deprecated. Instead of these methods I should use the Loader class for example (CursorLoader). As far as I know CursorLoader must be backed by a ContentProvider. The Android SDK recommends ContentProvider only if I want to share my data. But I just want to write a simple app, where no data should be shared.
In all my research I just find tutorials about Loaders in combination with ContentProvider. The SDK says that I can also write my own Loader over the class AsyncTaskLoader. Does someone already have some experience how to implement such a Loader? Are there any best practices? Are there any good tuturials how to implement such a Loader?
Or is it just better to implement a ContentProvider, so I can use the CursorLoader (this means a lot of work for just having a managed cursor)?
To make the ContentProvider private use android:exported="false" in your manifest.
ContentProviders are easier than you think and are the suggested way by the Android team. See http://responsiveandroid.com/2012/03/19/using-an-android-cursor-loader-with-a-content-provider.html for a good example of creating a ContentProvider.
I have used ArrayAdapter to populate the Spinner. I want to know if it can be done using a SimpleAdapter? Please guide me in the right direction.
Thanks in advance.
If my answer here (including the follow-up comments) is correct, then you may need to add a ViewBinder to the SimpleAdapter. You can google for examples of ViewBinder implementations -- it doesn't seem to be very complicated.
EDIT: You do need the ViewBinder, at least for some earlier versions of AndroidOS (I found it's not needed for 2.2). See the other answer, and perhaps also my blog post about this.