我在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()方法中重用视图.这真的很简单.
更新