【发布时间】:2013-08-25 20:51:40
【问题描述】:
我正在开发从服务器同步数据的 Android 应用程序。
服务器有数千条记录,我存储在SQLite Database.
但现在我在自定义TableView 中成功显示记录,但它非常耗时,我黑屏了 10 到 20 秒。
我的代码是:
/**** FILL ALL DATA OF PRODUCT WHICH IS AVAILABLE IN THIS COMPANY ****/
dbHelper.open();
productCursor = dbHelper.getProduct(companyid, isDistributor);
if(productCursor.getCount() > 0)
{
productTable.setVisibility(View.VISIBLE);
(view.findViewById(R.id.productIfNoAvailable)).setVisibility(View.GONE);
TableRow row= null;
com.salesman.library.ImageLoader mImageLoader = new com.salesman.library.ImageLoader(context);
ImageView prodImage;
TextView prodName;
EditText prodRate = null;
EditText prodQty = null;
EditText prodDisc = null;
/** For every time add new row in Table Layout **/
productTable.removeAllViews();
rowid = 0;
int catid = 0;
while (productCursor.moveToNext()) {
final View childView = inflater.inflate(R.layout.list_product_view, null);
row = new TableRow(context);
catid = productCursor.getInt(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_CATEGORY_ID));
int concatid = Integer.parseInt(catid+""+rowid++);
row.setId(concatid);
row.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
double productRateValue;
double productDiscountValue;
productRateValue = productCursor.getDouble(productCursor.getColumnIndex("rate"));
productDiscountValue = productCursor.getDouble(productCursor.getColumnIndex("discount"));
/*** Image ***/
prodImage = (ImageView) childView.findViewById(R.id.productImage);
String path = productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_IMAGES));
mImageLoader.DisplayImage(path, prodImage);
prodName = (TextView) childView.findViewById(R.id.productName);
prodName.setText(productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_NAME)));
prodQty = (EditText) childView.findViewById(R.id.productQuantityValue);
prodQty.setText("");
prodRate = (EditText) childView.findViewById(R.id.productRateValue);
prodRate.setText(String.valueOf(productRateValue));
prodDisc = (EditText) childView.findViewById(R.id.productDiscountValue);
prodDisc.setText(String.valueOf(productDiscountValue));
prodRate.setFocusable(isRateEditable);
prodRate.setEnabled(isRateEditable);
prodDisc.setFocusable(isDiscountEditable);
prodDisc.setEnabled(isDiscountEditable);
row.addView(childView);
productTable.addView(row);
if(productSharedPref.getBoolean("saved", false))
{
View rowView = productTable.findViewById(concatid);
String prodRatePrefValue = productSharedPref.getString("rate"+concatid, "");
String prodQtyPrefValue = productSharedPref.getString("qty"+concatid, "");
String prodDiscPrefValue = productSharedPref.getString("discount"+concatid, "");
if(!prodQtyPrefValue.equals("") || prodQtyPrefValue != null)
((EditText)rowView.findViewById(R.id.productQuantityValue)).setText(prodQtyPrefValue);
if(!prodRatePrefValue.equals(""))
((EditText)rowView.findViewById(R.id.productRateValue)).setText(prodRatePrefValue);
if(!prodDiscPrefValue.equals(""))
((EditText)rowView.findViewById(R.id.productDiscountValue)).setText(prodDiscPrefValue);
}
}
如何减少这个时间。
请帮忙。
【问题讨论】:
-
你可以使用
CursorLoader -
1000 条记录。也许不是一次获得所有这些,而是一次获得有限数量的。然后展示给用户。如果用户滚动浏览所有这些行,则加载另一组行。就这样继续下去。我不知道该怎么做,但看起来你可能会想到。
-
是的,谢谢,但如何执行此操作。
-
抱歉,您的大部分方法都是错误的。您应该使用动态重用视图的 ListView。这样,您将无缘无故地创建数千个视图以保留在内存中,从而减慢您的进程,并且可能会使应用程序在具有 OutOfMemory 的旧设备上崩溃。在 Google 上找到关于 ListView、ListAdapter 和 CursorAdapter 的好教程。这就是你必须使用的。
标签: android sqlite android-tablelayout