0

我正在开发从服务器同步数据的 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);
                }
            }

如何减少这个时间。

请帮忙。

4

1 回答 1

0

您可以使用异步任务......它的作用是加载一些项目并显示屏幕......然后它会在处理新项目时继续使用新项目更新列表

于 2013-08-25T12:51:40.383 回答