java 多线程 listview_java – ListView和CursorAdapter对大量数据的性能问题

博客作者遇到一个性能问题,他们的SQLite查询加载ListView时速度缓慢,特别是滚动时。他们使用了一个自定义CursorAdapter和SQLiteOpenHelper来获取数据。通过查询整个表并按特定列排序,他们发现加载6行数据和滚动列表需要很长时间。解决方案是创建一个只加载当前视图所需内容的自定义适配器,并在getView()方法中重用视图,从而显著提高性能。
摘要由CSDN通过智能技术生成

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

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

查询就像这样SELECT * FROM [table] ORDER BY [column] DESC;

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

使用我自己的SQLiteOpenHelper子类打开db

创建游标

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

}

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

}

}

}

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

因此,如果您可以从此代码中说出来,是否有任何原因,为什么要加载6行项目(一个屏幕高度)和滚动刷新(当您向下滚动一个项目时意味着)最多需要1分钟?

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

最佳答案 我创建了一个自定义适配器,它只加载活动视图所需的任何内容,并在getView()方法中重用视图.这真的很简单.

更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值