Kyle
Kyle

Reputation: 1

Limit number of items in ListView from Adapter

I currently am using a SimpleCursorAdapter with a ListView that pulls items using a CursorLoader from a ContentProvider and SQlite DB. The database contains around 300 items and I want to limit the number of its the Adapter passes the Listview so the ListView doesn't present all items from the Database. I want to avoid putting a limit on the db.query but rather hold items in the Adapter or limit the ListView. I haven't found an elegant solution to this. Any thoughts?

Upvotes: 0

Views: 2355

Answers (2)

Sam
Sam

Reputation: 86948

The database contains around 300 items and I want to limit the number of its the Adapter passes the Listview so the ListView doesn't present all items from the Database.

Cursors are rather efficient and 300 is a relatively small number so you should be able to just fetch all 300 at once. They are only drawn when the ListView scrolls. So if you see slow performance the problem might be in rendering the rows, not the Cursor.

I would like to have the data fetched by the adapter and have the ListView only display it when reaching end of visible items, for instance loading 25 items at a time.

This is simple enough consider a query like:

//db.query(tableName, columns, where, whereArgs, groupBy, having, orderBy, limit);
int limit = 25;
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null, String.valueOf(limit));

When you hit the bottom of your ListView, add 25 more to limit and run the query again:

limit += 25;
cursor = db.query(TABLE_NAME, null, null, null, null, null, null, String.valueOf(limit));

And so on, until limit >= cursor.getCount().

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93614

Make a custom adapter, and override the getCount function to return min(actual_number, max_you_want);

Upvotes: 2

Related Questions