Async task with SimpleCursorList (Android sql select) - android

I'm relatively new to Android but I just cant google this. I have following situation:
quite large SQL db on android (need to select and load about 2000 records to ListActivity)
I use SimpleCursorAdapter so far BUT... it doesn't allow me to load data asynchronously with AsyncTask (SimpleCursorAdapter has no "add()" as e.g. ArrayAdapter does)
I know how to make it work with ArrayAdapter but then I lose the ID attribute every time the time is clicked and I want to do it the "clean" way and keep the id (not save it some place hidden)
===> For now user has to wait till all db output is parsed into GUI, it takes some time. How can I fix it to make it run faster ? I need something like SimpleCursorAdapter.add(item) or extend it but not sure ...
thnx

You should consider having some pagination mechanism, not loading everything in an ArrayAdapter but better, returning a simpleCursorAdapter with just a subset of size N of your records. When the user will reach the last row, display a button to increase N and refetch the data from your database.

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.

get bigdata from mysql for android table

on which way I could load this data in my androidapp.
whats your expression, what do you think is the best way.
maybe I could do something like, loading 100 entries and wait until the user has seen the last one and then load again the other one. but how could I do this.
is it the best way to get data from sql with a php script or is there a better way?
There have 2 ways:
load all data into cursor
because the cursor most hold 2M data in memory,
when you scroll listview down/up,it will auto requery().
And there is no performance/memory issue by this way.
load data into cursor by page
by this method,need youself requery()
when use the first way or second way?
if you don't delete/update data when scroll listview,
i sugeest you use the first way.
else use the second way.

Android database search data on EditText TextChange

I am building an android application with a database that contains more than 20,000 entries.
When i retrieve data from the database, especially when searching for data, it seems to be working slow.
Especially, when i search data based on an editText. Every time editText TextChange(), i query :
Select * from mytable where data='mydata'
And it runs slowly.
I really don't know how to make it work faster.
Hope anyone can help me!
You can create an index on just that column to make selection work faster. If your column is the second one in an existing index, this index cannot be used most efficiently.
CREATE INDEX idx_mytable_word ON mytable(word)
May be you initialize db connection every time?
Try to add pagination. it's make your app faster.

pagination in listview

Thanks in advance.
I am developing a Car Review Application, where user can log in and displayed all the review from the Database. All the the data is being stored in MYSQLdatabase first. I am using json to connect to the MYSQLdatabase and SQLiteDatabase. But the problem is that, after log in the application screen huge no. of data is coming from the server and it is being inserted in our SQLite Database.
After that it is being retrieved from database and displayed in the Application Screen in a list view, it is taking a longer time to displayed all the data in list view. In that case, I am using a SimpleCursorAdapter to retrieve all the data from database.
So is there any way like pagination or something like that to make the data retrieval faster.
Please help me by giving some source code.
You can use something like:
Page 1:
SELECT * FROM YOUR_TABLE LIMIT 20 OFFSET 0
Page 2:
SELECT * FROM YOUR_TABLE LIMIT 20 OFFSET 20
Reference: http://sqlite.org/lang_select.html
You can use the concept of Asynchronous tasks along with SimpleCursorAdapters.
"AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers."
Here's what you can do:
1) Retrieve only 1st 10/15 items in the 1st query.
2) Fire another query as a background task, while user is checking out first 10/15 items.
This will certainly make the User experience faster
Using the LIMIT keyword from MYSQL you can achieve pagination.
LIMIT allows you to control the number of rows returned by query:
Example:
to show first 10 records
SELECT * FROM Student LIMIT 10 //for first time
to show rows between 10 and 20
SELECT *FROM Student LIMIT 9, 10 //after showing the records first time
LIMIT works for SQLiteDatabase also

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)

Categories

Resources