Bogdan Alexandru
Bogdan Alexandru

Reputation: 5542

Loading a BIG SQLiteDatabase in a ListActivity

I'm working on an Android project I need to finish very fast.

One of the app's features is loading a SQLite database content and listing it in a ListView inside a ListActivity.

The database contains a few tables, among which 2 are very large.

Each item in the database has many columns, out of which I need to display at least 2 (Name, Price), although preferably is 3.

This might seem a pretty easy task, as all I need to do in this part of the app is read a database and list it. I did this without any problems, testing the app versus a small sample database.

In my FIRST version, I used a Cursor to get the query, then an ArrayAdapter as the list's adapter, and after the query I simply loop the cursor from start to end, and for each position I add the Cursor's content to the adapter.

The onItemClickListener queries the database again versus other parameters (basically I open categories) so it clears the adapter, then loops the Cursor and adds its content to the adapter all over again.

The app worked like a charm, but when I used a real-life, big database (>300MB) I suddenly got my app taking very long to display the contents, and sometimes even blocking.

So I did some research and started using a SimpleCursorAdapter that automatically links the contents of a Cursor to the ListView using the usual parameters (String[] from, int[] to etc., where I used android.R.layout.simple_list_item_2 and android.R.id.text1 and text2).

Problem is, is doesn't change much the time to load.

I've came across some suggested solutions on different web sites and tutorials, most of them using, in one way or another, the AsyncTask class. I tried implementing this manually myself but it's hard to keep track of multiple threads and I failed.

Tutorials keep telling how to do this with content providers, but I found nothing clear bout my specific situation: very big SQLite database -> read to ListView.

Now my head is filled in with notions like LoaderManager, LoaderAdapter etc, all mixed up and confused in my head.

Can anybody please provide me a complete, nice, clean solution to do this "simple" task?

Again: I want to read a BIG SQLiteDatabase and display it in a ListView. I want the app NOT to block.

I need a class that has a member function that takes as parameter a query and the ListActivity's context and takes itself care of displaying the result of the query in the view.

Please don't provide me abstract answers. I'm running out of time and I'm very confused right now and I need a clean complete solution.

You're my only hope.

Upvotes: 1

Views: 1728

Answers (3)

Rohit
Rohit

Reputation: 1001

If you query such large database it will take tym, you need to find a smart way,

Like limit you database query to get first 10 or 30 items and then maintain,once last item is reached query rest 30 items and bind them

Refer this tutorial, it will teach you how to add data dynamically in a list view

http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

The above list has expired chk this

http://mobile.dzone.com/news/android-tutorial-dynamicaly

Upvotes: 3

user1381827
user1381827

Reputation:

you can also use :

public class TodosOverviewActivity extends ListActivity implements
    LoaderManager.LoaderCallbacks<Cursor>

check this link for more details.

Upvotes: 1

SAURABH_12
SAURABH_12

Reputation: 2300

If you query large database it will take time to fetch data and show it on List View. So it is better to populate data at run time. You can use Lazy Adapter concept to load data . This link1 may be useful for You.

Thanks

Upvotes: 1

Related Questions