java - ListView 和 CursorAdapter 对大量数据的性能问题

标签 java android sqlite

我在 sqlite 表中有大约 4k 行,表有 7 列。

我使用自己的 CursorAdapter 创建了工作 ListView。

查询是这样的SELECT * FROM [table] ORDER BY [column] DESC;

表有第一列 _id INTEGER PRIMARY KEY 但排序由另一列完成。

使用我自己的 SQLiteOpenHelper 子类打开数据库

创建游标

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");

将其传递给 ListView

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

在 ItemsAdapter 中我重新实现了

private LayoutInflater inflater;

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

@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

}

我的包装类

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);
        }
    }
}

在 ListView 中使用了相同的行初始化代码,并且在那里工作得非常好。

因此,如果您可以从这段代码中得出结论,为什么加载 6 行项目(一个屏幕高度)和滚动刷新(意味着当您向下滚动一个项目时)最多需要 1 分钟?

仅加载 ListView 最多需要 2 分钟,然后大约一半时间用于向下/向上滚动一个列表项。性能问题可能在哪里?

最佳答案

我会创建一个自定义 Adapter,它只加载 Activity View 所需的任何内容,并在 getView() 方法中重用 View 。这真的很简单。

更新

我找到了一个很好的例子,你应该可以使用: http://android.amberfog.com/?p=296

关于java - ListView 和 CursorAdapter 对大量数据的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7650968/

相关文章:

android - 如何在处理中提高 Android 模式下的帧速率?

sqlite - 在哪里以及如何使用 SQLITE 的 NULLIF(X,Y) 函数?

sqlite - Phonegap-无法还原插件“io.litehelpers.cordova.sqlite”

java - 网 bean : how to resize the UI Window to screen Size

java.lang.NumberFormatException : Invalid int: in display the image from sd card

Android ImageView 缩放和平移问题

android - 按项目过滤 SQLite 数据库 "popularity"- Android

eclipse 每隔几分钟死一次,最新的 jdk 说 'out of space in CodeCache for adapters'

java - 返回java中的main方法

android - FCM 组还是 token 数组?