0

好的,我正在从服务器获取图像并在带有标题的gridview中显示它们...我使用baseadapter for gridview ...一切运行良好...我只有一个问题,我需要在获取数据时显示进度对话框来自服务器并填充在gridview上......我现在使用AsyncTask显示进度对话框,但它冻结了几秒钟(如10)秒,然后gridview显示......

这是我的 baseadapter 类:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = listInflater.inflate(R.layout.grid_adapter_view, null);
        holder = new ViewHolder();
        holder.gridImg = (ImageView) convertView
                .findViewById(R.id.grid_item_image);
        holder.gridTitle = (TextView) convertView
                .findViewById(R.id.grid_item_label);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.gridTitle.setText(catItems.get(position).getName());
    try {
        URL url = new URL(catItems.get(position).getImg());
        Bitmap img = BitmapFactory.decodeStream(url.openConnection()
                .getInputStream());
        holder.gridImg.setImageBitmap(img);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        holder.gridImg.setImageResource(R.drawable.no_image);
        e.printStackTrace();
    }
    return convertView;
}

public class ViewHolder {
    ImageView gridImg;
    TextView gridTitle;

}

这是我使用的异步任务:

mytask = new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            main_grid.setAdapter(adater);
            if (progress.isShowing()) {
                progress.dismiss();
            }

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            progress.show();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            JSONArray jArray = request.getCategories();
            try {
                for (int i = 0; i < jArray.length(); i++) {
                    CategoriesDetail catDetail = new CategoriesDetail();
                    JSONObject jobj = jArray.getJSONObject(i);
                    catDetail.setId(jobj.getString("id").toString());
                    catDetail.setName(jobj.getString("name").toString());
                    catDetail.setImg(jobj.getString("img").toString());
                    catDetail.setOrder(jobj.getString("order").toString());
                    items.add(catDetail);
                }
                adater = new CatGridAdapter(MainActivity.this, items);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                progress.dismiss();
                Toast.makeText(MainActivity.this,
                        "Error: " + e.getMessage(), Toast.LENGTH_LONG)
                        .show();
                return false;
            }

        }

    };

请告诉我我在哪里做错了什么......

4

1 回答 1

1

您正在 UI 线程上执行网络操作。

BitmapFactory.decodeStream(url.openConnection().getInputStream());

看看这个:http: //developer.android.com/training/displaying-bitmaps/process-bitmap.html

编辑: 而不是解码流,getView()只将默认图像设置为gridImgand trigger loadBitmap(catItems.get(position).getImg(), holder.gridImg)

public void loadBitmap(String url, ImageView imageView) {
    new BitmapWorkerTask(imageView).execute(url);
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params) {
        return BitmapFactory.decodeStream(params[0].openConnection().getInputStream());
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

这一切都在上面的链接中描述。您应该真正阅读它以更好地理解 android 中的线程。

于 2013-01-10T23:09:40.527 回答