一堆记录显示在自定义视图android但很耗时

时间:2013-08-25 12:42:42

标签: android sqlite android-tablelayout

我正在开发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);
                }
            }

如何缩短这段时间。

请帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用异步任务.....它的功能是加载一些项目并显示屏幕...然后它将继续使用新项目更新列表,当它们被处理时