Create a cursor to point to an array - android

I'm trying to create a search suggestion content provider, and I need to to return its results as a cursor (for the contentprovider API of query).
The thing is, my data isnt saved in a SQLite database but in an array.
I need to create a cursor object which binds to an array, but I haven't seen any sample that does that.
Does anyone know what's the simplest way to bind a cursor to an array?

Related

How to create Cursor data without getting data from the DataBase in Android application?

In my Android application, I am using Sqlite DataBase to store the data from the server. I am using ContentProvider and ContentResolver to access the data from the DataBase and using CursorAdapter to bind the data to the ListView. As soon the data is inserted into the DataBase, CursorAdapter will be notified to update the ListView. Also, whenever I scroll the ListView, I get new data from the DataBase table and ListView will be updated. But once I reaches to the end of the table row, I need to fetch the data directly from the server without storing into the DataBase and update it in the ListView. Now as I am using CursorAdapter which accepts Cursor data, how can I bind a new set of data which is not coming from the DataBase? Is is possible to create Cursor data without getting data from the DataBase and use ChangeCursor() method in the ContentProvider to update the ListView? If not, is there any other method to achieve the same?
Is is possible to create Cursor data without getting data from the
DataBase and use ChangeCursor() method in the ContentAdapter to update
the ListView?
Yes, you can create cursor using MatrixCursor. If you will have to merge MatrixCursor with database Cursor use MergeCursor.

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.

do I need to store the cursor for a listview at class level?

My listview uses a cursor via an adaptor. Do I need to keep this cursor variable at a class level for any reason as the Listview adaptor stores it anyway?
Actually i don't understand why you want to do this if you want to access records again then just store them after fetching through cursor and then you can access them any where in your project.
If you keep this cursor variable then you again want to traverse the database in order to retrieve or other operations . But i think make cursor for you function level access records(you can store them in Vector or ArrayList) then use that stored records instead of using cursor.
If you want to tore cursor make it of class level and access it in other classes (but remember the function reinitialize cursor must call first before calling cursor in other classes).
Hope this explanation works for you...

Android - Best way to reference data in a database

I have an app that uses an sqlite database. Currently I have a database helper class that I call from within my main class to access the database. I then temporarily store the items I retrieved in a cursor. Every time I want to access my database I have to open the db, create a new cursor, query the db and add the results to the cursor, pull the information out of the cursor, close the cursor, close the db, then display to the user.
Now I'm accessing the db helper class every time I want to access the db
I'm creating a new cursor every time
And I'm making sqlite queries.
I was wondering if I should change my code to do something like:
Open DB
Store all data to a cursor
close db
then whenever I want to access the information I can instead just reference the cursor eliminating the need to call the db helper class again.
Or would it be more efficient to instead store the data to an array doing something like:
open db
store data to a cursor
loop through cursor and store data into an array
close cursor
close db
What would the difference be (processing time, and memory use) between these and is there an even more efficient way of doing this that uses the least resources? Is it faster to use an array or a cursor? I don't want to waste cpu time calling unnecessary functions or use more memory storing bloated objects.
In your activity if you want to execute a select query once and you using that data to your activity then you have to make a ArrayList or Hashmap from Cursor and then you have to close your cursor.
But if you have to access a database many times and you are getting a different different results by executing a query then you have to use cursor and fetch data from cursor update to UI and close Cursor.
You are using Helper class so it is the best choice so you don't have to wonder about processing.
when ever you want to fetch data then just make a object of Helper class,Open connection,execute query and return data,fetch data from cursor in your activity,Update UI,close database.
this is the normal way and also it is a good way.

Content Provider filtering query, filtering Cursor

I got following problem, I need to use a Content Provider to read a
Database of an other App.
first I want all rows, and after analyzing the data only e.g. the rows
from _id = 1, 3 and 5.
how can I call a Content provider and select only these rows?
or is it possible to create a subset Cursor form an given Cursor?
Thanks in advance.
If you're talking to another app, I assume you're querying the other app's ContentProvider to get the data from them in the first place.
In this situation, the cleanest answer seems not to build your own ContentProvider that filters/wraps theirs. Instead query their ContentProvider from your application directly, and use the select clause in your query() to specify the conditions that define the subset of data you want to be given.

Categories

Resources