Which adapter choose? - android

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.

Related

Android SQLITE search VS ArrayList search for small amount of dataset

Suppose, In my app I have a sqlite table that can contain at most 20 row. Each row has 2 column(id, name). Where I frequently need to search by Id to get Name. For this frequent need I have two solution:
Solution 1: Get rows in a arraylist<model> and then find name from array.
Solution 2: Every time search on sqlite table.
Now please give your opinion which one is better?
Remember again, I need this search in my recycleView item, so it call so frequently.
Thanks
I don't really get what is your real intent, but if your task is to search by id often, I would use
LongSparseArray<String> idsToNames; // or LongSparseArray<Model>
Which will map primitive long to Strings in a more memory-efficient way than Map and will have a better performance than ArrayList when searching.
The advantage over querying SQLite here is that you can do it in a blocking manner instead of having to query database on a background thread every time the lookup runs.
The disadvantage is that whenever data changes in SQLite, you will have to rebuild your idsToNames map. Also, if the number of entries in SQLite will eventually grow, you will end up in a large collection. So I would recommend this approach only if the updates to the database during this session will not happen, and if the data size is always predictable or fixed.

In Android SQLite, working directly with Cursor is more memory efficient than creating Model Objects?

In most of the Android sample codes, populating a ListView from SQLite database is done in two ways,
Prefetch data to List - Execute query, create Model objects for each row then add it to a List and close the Cursor, then populate ListView with List.
Without List and Model objects - Execute query and populate ListView by following the Cursor using moveToFirst, moveToLast, move, as required.
Now I want to know, which of the above method is more memory efficient, in Android ?
The Cursor approach is more memory efficient:
Suppose you have 1000 entries in your database and you have a ListView which can show 10 entries at the same time. If you create a list at first, you'll have to create 1000 model objects (each of which in turn consists of several objects depending on the number of columns of your table) and the listview creates additional 10 views (actually some more, depending on the layout of the list) for displaying the 10 items. Now when the user scrolls the list, in your Adapter you end up copying data from your model objects to the list item views currently in view.
On the other hand, if you use a CursorAdapter, whenever you have to fill a list item with data, you are provided with the Cursor holding exactly the data for that row and you can simply pick the data of the columns you actually need to be displayed in the list item. No need for creating the 1000 model objects.
From a code readability perspective, a model approach would be better because working with Cursors is quite low level, you'll need to know the names of the columns in the database and so on.
I think you need to use Service or at least Thread/Async so your UI thread will not be blocked. Service is better because people can go to other apps while downloading. You can use BroadcastReceiver to communicate with your running Service.

Populating ListView with ORMLite

I need to populate ListView with List of objects returned from my Dao object.
The items get returned after 3 seconds, obviously to much time for the user to wait...
I'm using BaseAdapter as the ListView adapter.
2 questions:
How can get rid of the 3 seconds waiting time? Should I just retrieve the entire list of objects in a seperate worker Thread and display dialog in the meanwhile? Is there any mechanism that allows me to get the first, let's say... 20 records, display them and fetch the rest of the records while the user scrolls down the list?
If I would use cursors, rather than ORMLite, the list would then query the DB as the user scrolls down the list, releasing the objects of the hidden cells and the cells themselves, and not keeping all the objects of the cursor in the memory. How can I achieve this behavior with ORMLite?
I hope I was clear enough, despite the bad English ;)
Thanks.
You might want to load the data in an AsyncTask, and display a ProgressDialog while it loads. Lot of Android apps do this.
Cannot OrmLite return a DataProvider instead of the while list? (I too wanted to look into ORM on Android but the management decided against it "Its slow", but I still badly want it)

Updating a ListView with frequently changing data in Android

The question posted in this thread asks how to update a ListView when the data has changed. It assumes, however, that the data that is modified in a ListView is stored in a database. Therefore, the ListView UI can be updated by simply calling requery() after the changes have been committed to the database.
What should one do when modified data of a ListView is not stored in a database?
For example, if each item in a ListView is showing the distance to a particular landmark based on the user's current GPS coordinates, what is the correct way to update the view with the updated distances as the user moves about? Should the distances simply be stored in the database as a matter of convenience, so that requery() will update the UI? This does not seem like the correct approach if it is not necessary to persist (frequently changing) GPS data.
Edit: To clarify, I'd specifically like to address the situation where some of the data is stored in a database (the coordinates of the landmarks, for example), however, the frequently changing data is computed on the fly (e.g., the distances).
ArrayAdapter can be used instead of CursorAdapter.
The notifyDataSetChanged() method can be used to tell the ListView to refresh itself by requerying the ArrayAdapter assigned to it.
I suggest using ArrayAdapter instead of CursorAdapter (which is used to populate a ListView based on cursor > database query)
ArrayAdapter
You should update the array data storing your coordinate data. When it changes call adapter.notifyDataSetChanged() to have your list updated with the latest array data
Hope that helps

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