1

我正在编写一个需要从 api 收集数据并显示它的应用程序。我动态创建视图并将其添加到线性布局,如果我使用线程和处理程序,它工作正常,但它在 AsyncTask 中表现得很奇怪。

以下是我的 OnPostExecute

protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        productInfoFetched = productInfoFetched
                + productDetailTempList.size();

        insertItems(productDetailTempList);
        tvLoading.setVisibility(View.GONE);
        if ((productInfoFetched != 0)
                && (productInfoFetched == totalProductCount)
                && (totalProductCount > 10))
            btnSearch.setVisibility(View.VISIBLE);
        else
            btnSearch.setVisibility(View.GONE);
        downloading = false;
        Log.v("in download info task", "Last line of post exe");
    }

insertItems(productDetailTempList); 获取应该更新视图的 Arraylist。确实如此,但在此行执行 Log.v("in download info task", "Last line of post exe"); 之前什么都不会显示

我的 insertItem 函数如下:

public void insertItems(
        final ArrayList<ProductDetailsDTO> productDetailList2) {
    // infoTaskObj = null;
    infoTaskObj.cancel(true);
    Log.v("AT status", infoTaskObj.getStatus().toString());

    for (final ProductDetailsDTO product : productDetailList2) {

        final Holder holder = new Holder();

        View row = ((LayoutInflater) viewObject).inflate(
                R.layout.product_id, null, false);

        holder.product_image = (ImageView) row
                .findViewById(R.id.product_image);
        holder.product_name = (TextView) row.findViewById(R.id.name);
        holder.sku = (TextView) row.findViewById(R.id.sku);

        holder.price = (TextView) row.findViewById(R.id.price);
        holder.ivOverlay = (ImageView) row.findViewById(R.id.ivOverLay);
        holder.edbuton = (Button) row.findViewById(R.id.ibtnDel);
        lstButton.add(holder.edbuton);
        Log.v(TAG, "before immage set");
        try {
            URL url = new URL(product.url);

            Bitmap bmp = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());
            Bitmap btmp = Bitmap.createScaledBitmap(bmp, 80, 80, true);

            holder.product_image.setImageBitmap(btmp);

        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        holder.product_name.setText(product.name);
        holder.sku.setText(product.sku);
        holder.price.setText(product.price);
        if (product.status.equalsIgnoreCase("0")) {
            holder.ivOverlay.setVisibility(View.VISIBLE);
        } else {
            holder.ivOverlay.setVisibility(View.GONE);
        }

        if (ed_flag) {
            holder.edbuton.setVisibility(View.VISIBLE);
            row.setOnClickListener(null);

        } else {
            holder.edbuton.setVisibility(View.GONE);
            row.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Log.v("Row clicked", "yes");
                    Object obj = view.getTag();
                    String tag = (String) obj;

                    try {
                        // Intent child = new Intent(productListActivity,
                        // ProductThumb.class);
                        if (holder.ivOverlay.getVisibility() == View.VISIBLE)
                            holder.ivOverlay.setVisibility(View.GONE);
                        else
                            holder.ivOverlay.setVisibility(View.VISIBLE);

                        Intent child = new Intent(productListActivity
                                .getParent(),
                                ProductCompleteDetailActivity.class);
                        child.putExtra("ProductDetailDTOList",
                                productDetailList);
                        child.putExtra("ProductDetailDTO", product);
                        productListActivity.startActivity(child);

                        // Intent intent = new
                        // Intent(productListActivity.getParent(),ProductCompleteDetailActivity.class);
                        // TabGroupActivity parentActivity =
                        // (TabGroupActivity)
                        // productListActivity.getParent();
                        // intent.putExtra("ProductDetailDTO", mData);
                        // parentActivity.startChildActivity("DisplayDetail",
                        // intent);

                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                    // Toast.makeText(parent.getContext(),
                    // "Product id: " + mData.get(position).productId,
                    // Toast.LENGTH_SHORT).show();
                }
            });

        }
        holder.edbuton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // TODO Auto-generated method stub
                String status;
                status = product.status;
                Log.v("Id of product clicked in array list",
                        product.productId);
                Log.v("Status of product", status);
                if (status.equalsIgnoreCase("1")) {
                    disableProduct(product.productId, "0");
                    product.status = "0";
                } else {
                    disableProduct(product.productId, "1");
                    product.status = "1";
                }
            }
        });

        llMainView.addView(row);
        scroll = true;
    }

}

我认为每个项目都应该在 llMainView.addView(row); 时开始显示 该行执行。但事实并非如此。它等待 OnPostExecute 完成,然后所有项目一起显示。我这里有什么问题吗?

4

0 回答 0