开发者

Android SimpleCursorAdapter - Limiting ListView results

开发者 https://www.devze.com 2023-02-15 12:52 出处:网络
I apologize in advance as I am away from my laptop and cannot produce any code examples, but hopefully I can be clear enough to have my question answered.I have a ListView backed by a sqlite db (via a

I apologize in advance as I am away from my laptop and cannot produce any code examples, but hopefully I can be clear enough to have my question answered. I have a ListView backed by a sqlite db (via a SimpleCursorAdapter). I am trying to implement functionality so the list only shows a certain # of elements from the db query at a time. I've done this by loading all data from the db in onCreate, and overriding the getCount method of SimpleCursorAdapter with the preferred # of elements when the list is initiated. I also have a button that loads more elements to the list. This is done by updating the count that getCount should return.

This works fine in adding more listview rows, however I'm seeing strange behavior with the contents in my list after I click the button. I'm seeing duplicate list elements and incorrect element contents, which are set in my bindview method. I'm开发者_JAVA技巧 assuming this had something to do with overriding the getCount method (i'm sure thee's a better way to do a "Load Next 25" function), as I am not seeing this behavior when I just show the whole db result set and don't override that method.

I guess my question is can overriding the getCount method result in funny behavior in the bindview method when you're dealing with a SimpleCursorAdapter? What's the best way to implement this type of functionality with db query results backing your ListView?

Thanks in advance...if needed I can provide code snippets tomorrow when I'm back at my laptop.


I think you can archive this by simply using a different query to get you results. SQLite supports a LIMIT operator which can returns the top n results of a query.

So your first query looks something like this: SELECT [your columns] FROM [your table] LIMIT 25

And when the button that loads the next 25 results is pressed, you create a new query with a limit of 50 and bind the resulting cursor to the SimpleCursorAdapter using the changeCursor() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消