views:

51

answers:

2

Hi, I'm getting some very jerky scrolling while using the code below to create a ListView from a Cursor. Is there something I'm doing wrong, or any way to improve the performance of this ListView?

bookmarksListView = (ListView)findViewById(R.id.bookmarks_listview); 
                        bookmarksDbCursor = bookmarkStore.getCursor(); 
                        startManagingCursor(bookmarksDbCursor); 
                        String[] bookmarksColumns = new String[3]; 
                        bookmarksColumns[0] = "TITLE"; 
                        bookmarksColumns[1] = "URL"; 
                        bookmarksColumns[2] = "ICONID"; 
                        int[] bookmarksViews = new int[3]; 
                        bookmarksViews[0] = R.id.title_text; 
                        bookmarksViews[1] = R.id.subtitle_text; 
                        bookmarksViews[2] = R.id.icon_view; 
                        bookmarksListAdapter = new SimpleCursorAdapter(this, 
                                        R.layout.list_item, 
                                        bookmarksDbCursor, 
                                        bookmarksColumns, 
                                        bookmarksViews); 
                        bookmarksListView.setAdapter(bookmarksListAdapter); 
                        bookmarksListView.setOnItemClickListener(new OnItemClickListener() 
{ 
                                @Override 
                                public void onItemClick(AdapterView<?> parent, View view, 
                                                int position, long id) { 
                                        Toast.makeText(BookmarkHistoryTabActivity.this, "Clicked ID " + 
Long.toString(id), Toast.LENGTH_SHORT).show(); 
                                } 
                        }); 
                        registerForContextMenu(bookmarksListView); 

Note: bookmarkStore.getCursor() returns an unmanaged cursor from a sqlite database containing the columns from bookmarksColumns.

The icons I'm loading (from R.drawable) aren't the issue - I've tried turning them off and it has no effect. I've encountered a similar scrolling issue with a custom ListAdapter extending BaseAdapter I wrote a while ago, and it was caused by not properly setting the view's values in getView() I resolved it using the fix posted at http://stackoverflow.com/questions/1320478/how-to-load-the-listview-s..., but that was for a custom adapter class, and I'm not really sure how to implement that fix when I'm using a stock SimpleCursorAdapter, or if that fix even applies to this situation.

EDIT: The query returning info on the database looks like this, in case that helps:

public Cursor getCursor() {
            openDB();
            Cursor c = mDB.query(DB_TABLENAME, new String[]{"_id", "ICONID", "DATE", "TITLE", "URL"}, null, null, null, null,"DATE DESC");
            return c;
    }

EDIT2: I have no idea why, but this behavior only occurs in android 1.6+ I've tried changing my query, removing the sorting, etc. for my cursor, nothing seems to help. The database is very small (<20 items) and only contains the columns present in the query above.

A: 

Your code looks fine. Therefore it is probably in a different area. My guess (since I can't see anything else) is that your database itself is having problems. Try indexing your database, or simplifying the queries in it. Also try doing the SQLite compress to get rid of any garbage that might be hiding in the database.

Moncader
Would the issues you describe still cause problems if there are very few items in the database? I'm still seeing this behavior even with fewer than 10 items in the database.
QRohlf
More than a problem with the number of items in the database it would be more of a problem with the query itself. For example if you're doing lots of joins, unions, group by/distinct and/or sql functions. If its just plain select * from whatever then there shouldn't be a problem. Unless the database is 'really' messy (Not your fault, running a clean on that database will fix that).
Moncader
A: 

We've determined that this is an issue with Android versions 1.6+, rather than our code, and are leaving it as-is.

QRohlf