pagination in listview - android

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

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.

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.

SQLite: How to limit the number of rows based on the timestamp?

I successfully used the following BEFORE INSERT trigger to limit the number of rows stored in the SQLite database table locations. The database table acts as a cache in an Android application.
CREATE TRIGGER 'trigger_locations_insert'
BEFORE INSERT ON 'locations'
WHEN ( SELECT count(*) FROM 'locations' ) > '100'
BEGIN
DELETE FROM 'locations' WHERE '_id' NOT IN
(
SELECT '_id' FROM 'locations' ORDER BY 'modified_at' DESC LIMIT '100'
);
END
Meanwhile, I added a second trigger that allows me to INSERT OR UPDATE rows. - The discussion on that topic can be found in another thread. The second trigger requires a VIEW on which each INSERTis executed.
CREATE VIEW 'locations_view' AS SELECT * FROM 'locations';
Since an INSERT is no longer executed on the TABLE locations but on the VIEW locations_view, the above trigger does no longer work. If I apply the trigger on the VIEW the following error message is thrown.
Failure 1 (cannot create BEFORE trigger on view: main.locations_view)
Question:
How can I change the above trigger to observe each INSERT on the VIEW - or do you recommend another way to limit the number of rows? I would prefer to handle this kind of operation within the database, rather then running frontend code on my client.
Performance issues:
Although, the limiter (the above trigger) works in general - it performs less then optimal! Actually, the database actions take so long that an ANR is raised. As far as I can see, the reason is, that the limiter is called every time an INSERT happens. To optimize the setup, the bulk INSERT should be wrapped into a transaction and the limiter should perform right after. Is this possible? If you like to help, please place optimization comments concerning the bulk INSERT into the original question. Comments regarding the limiter are welcome here.
This type of trigger should work fine in conjunction with the other one. The problem appears to be that the SQL is unnecessarily quoting the _id field. It is selecting the literal string "_id" for every row and comparing that to the same literal string.
Removing the quotes around '_id' (both in the DELETE and in the sub-SELECT) should fix the problem.

Async task with SimpleCursorList (Android sql select)

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.

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