tags:

views:

334

answers:

1

Hi all,

With the application i am working on i came with the following problem. I have a listview that should display data from a data base table. There are two scenarios that could happen:

  • Scenario one - the database table is filling dynamically and the listview also should dynamically display table's information e.g to grow depending on table size.
  • Scenario two - the table has been already filled and the listview has to just display the content

So the problem is how to accomplish this behavior using ListView and ListAdapter.

So far i have solution for each scenario but no for the two together.

  • Scenario one - Use AsyncTask and ArrayAdapter. In doInBackGround query the db periodically and pass the result to onProgressUpdate, than just fill the ArrayAdapter with the newly added values. But when came scenario two i query all the table than copy all values to the ArrayAdapter in one step which is pretty slow.
  • Scenario two - Query the db again the just use CursorAdapter. But in this case i can't update the ListView dynamically using CursorAdapter.

So any ideas how to implement this using one adapter, or just should i use ArrayAdapter and CursorAdapter depending on the case ?

+1  A: 

Scenario one - the database table is filling dynamically and the listview also should dynamically display table's information e.g to grow depending on table size.

Try:

Scenario 1a: Whoever is getting the data not only puts it in the database, but tells the activity "here's some new data to display".

Databases are great data stores but are lousy pipes. Your "Scenario one" tries to use the database as a pipe -- instead, pipe around it.

Now, if the case is that you have both in-the-database data and new data coming in, you'll need to stitch those datasets together. You can use my MergeAdapter for it: give it two ListAdapters, one representing your existing data (perhaps a CursorAdapter) and one representing the new data (perhaps an ArrayAdapter). It will render them as a combined entity.

CommonsWare
0 vote down checkI don't want to keep two datasets. Actually there is only one dataset just it has two different states.So any other ideas
Mojo Risin