CursorAdapter vs ArrayAdapter for a ListView - android

I want to fill my ListView with data that's going to come from the web in the form of JSON. The list should be theoretically infinite, with the app making requests for more data if scrolled to the bottom.
Should I use a Cursor or an Array(List) adapter to link my online database with my ListView?
More generally, what are the arguments to consider when choosing between cursor and array?

Well I think you should look at ContentProviders. They are more natural to the problem that you are trying to solve. You have to implement your custom Cursor which ContentProvider returns on a query request.
Ref:
http://developer.android.com/guide/topics/providers/content-providers.html

Related

Populating a Android ListView with MySQL data

I want to retrieve data from a MySQL database and populate a ListView in my Android application. I've seen answers on stackoverflow that use complicated methods of retrieving the data from a MySQL database using JSON objects. Instead, is there anyway I can use something similar to a MySqliteCursor to connect to a MySQL database? Is the Cursor Android class limited to using SQLite databases? If none of these options are possible, is there an easier way to populate a ListView with MySQL data?
See CursorAdapter class. It receive cursor, db column indicies and ids of widgets of view. Then it take data from db and populate corresponding fields
Yes, it receive Cursor from sqlite. You can easily implement adapter for any data- just know number of rows and query row under particular index. See RecyclerView tutorial (updated listview)

Which adapter choose?

I have two methods which read the same data from database, the first returns Cursor and the second returns List of objects.Now I show my items in activity using SimpleCursorAdapter and the first method, byt I can also use the second method and appropriate adapter.
Which of these two ways is beter to use and in the second way which adapter I should use?
P.S sorry for poor english
Definitely go with SimpleCursorAdapter. If possible, always use Cursor if your data comes from database, you save memory by not creating List of objects. Creating objects in Java is expensive with regards to time and memory consumption and you have to bear in mind you are on mobile platform with limited resources. If you are using List of objects for your ListView than use custom adapter extending from ArrayAdapter.
It's not always straightforward to use Cursor although your data comes from database. Let's say you store places in the database defined by its name and location and you want to display them in a ListView sorted by distance from current location. It makes it difficult to execute a query which returns sorted results unless you don't store relative distance in additional column. But you can get Cursor convert it to List of objects and sort this collection before sending it to your ListView.

showing Cursor Data on ListView using Cursor loader in Android

I am working on a task where I need to get the data from SQLite database and show it on a list view.I know that we can extend SimpleCursorAdapter and Override getView() method to show the data on list view,but I have seen in developers site that in 3.0 where we can use cursor loader to show data on list view, but it was said that Cursor Loader is used only for querying ContentProviders.Can I use Cursor Loader for querying SQLite DataBase. If so how can I do this, plz help me with some sample code...
As per Android API documentation, Cursor Loader is mainly for querying content providers and not for SQLLite queries and explicity saying if data is not content (shared between applications) use SQLLiteDatabase. So, I would strongly discourage using CursorLoader for SQLLite operations.
CHeck this sites:
http://www.codeproject.com/Articles/525313/Using-Cursor-Loader-in-Android
http://www.grokkingandroid.com/using-loaders-in-android/
also you can find that questions helpful:
Show Progress Indicator before fill listview using CursorLoader
ListView with CursorLoader and SimpleCursorAdapter isn't displaying data
Load data from multiple loaders/cursors into a listview in android

Display search results from both local DB and remote service

I have an app that stores items in a local database, displayed to the user using a ListView, also in the layout is an EditText that can be used to filter the list. All of this works fine, my issue is I would also like to include items returned by a web service (JSON) in this list also. I'm not having an issue parsing the JSON, my issue is simply how do I insert/add the results from the web service to the ListView?
The data for the ListView comes from a cursor handled by a SimpleCursorAdapter, I just can not figure out how to add the items from the JSON results to the Cursor (I don't actually think you can write to a Cursor outside of performing a Query).
If you don't want to insert the JSON results into the local DB, then one approach would be to use an ArrayAdapter to back the ListView. You can then populate the array by inserting results from the cursor and the JSON query.
Hope this helps,
Phil Lello

Autocomplete list from SQLite with criteria

Can someone point me to right direction, how to create an adapter for AutoCompleteTextView, which would be getting data from SQLite DB, using data, which user entered into the text field? I want to use the user-entered data to filter suggestions for autocompletion. I imagine that adapter should always take user-entered data as soon as changes appears and use it for fetching suggestions on-the-fly. Is that possible? So far I've seen many tutorials for autocompletion where static String arrays were used, but never seen them build dynamically. Is it possible to do it automatically or I need always fetch String array myself and pass as ArrayList to adapter on every AutoCompleteTextView change?
You might be looking for CursorAdapter. Use it just like an ArrayAdapter, but instead of feeding it with an ArrayList, provide a database Cursor. Google for CursorAdapter and you should get a lot more example codes.

Categories

Resources