开发者

Performance issue with ListView and CursorAdapter for large amount of data

I have about 4k rows in sqlite table, table has 7 columns.

I created working ListView with my own CursorAdapter.

Query is like this SELECT * FROM [table] ORDER BY [column] DESC;

Table has first column _id INTEGER PRIMARY KEY but ordering is done by another column.

For opening db using my own subclass of SQLiteOpenHelper

Creating cursor

mySQLiteOpenHelper pm = new mySQLiteOpenHelper();
SQLiteDatabase db = pm.getReadableDatabase();
Cursor c = db.query([tablename], new String[]{"_id", "[column]", "[column]", "[column]", "[column]", "[column]"}, null, null, null, null, "name ASC");

Passing it to ListView

ListView lv = (ListView) findViewById(R.id.list_items);
lv.setOnItemClickListener(this);
pa = new ItemsAdapter(ItemsActivity.this, c);

In ItemsAdapter I have reimplemented

private LayoutInflater inflater;

@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
    return inflater.inflate(R.layout.items_row, arg2,false);
}

and

@Override
public void bindView(View rtn, Context arg1, Cursor c) {
    item_name = (TextView) rtn.findViewById(R.id.item_name);
    item_description = (TextView) rtn.findViewById(R.id.item_description);
    item_catalog_id = (TextView) rtn.findViewById(R.id.item_catalog_id);
    item_true_price = (TextView) rtn.findViewById(R.id.item_true_price);
    item_display_price = (TextView) rtn.findViewById(R.id.item_display_price);
    item_button = (Button) rtn.findViewById(R.id.item_button);

    item = new MyWrapClass(c);

    // some work with item to fill up all UI items

}

MyWrapClass

public final class MyWrapClass {

    public String name = "";
    public String notes = "";
    public int id = 0;
    public String catalogId = "";
    public int price = 0;
    public int publicPrice = 0;
    public String groupPrice = "";
    public int order_count = 0;

    public MyWrapClass(Cursor c) {
        try {
            id = c.getInt(0);
            catalogId = c.getString(1);
            name = c.getString(2);
            price = c.getInt(3);
            publicPrice = c.getInt(4);
            groupPrice = c.getString(5);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

The same row init code was used in ListView and there it worked very good.

So if you can say from this code, is there ANY reason, why should load of 6 row items (one scree开发者_JAVA百科n height) and scroll refresh (mean when you scroll one item down) take up to 1 minute?

Just load of ListView takes up to 2 minutes, and then about half time to scroll one list item down/up. Where can be the performance issue?


I'd create a custom Adapter, that only loads whatever is needed for the active views and that reuses views in the getView() method. It's really quite simple.

Update

I found an excellent example, that you should be able to use: http://android.amberfog.com/?p=296

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜