0

我需要你的帮助!

在我的应用程序中,在GridView. 单元格显示图像,而不是默认的R.id.noimage和以前查看的图像。在我添加适配器以加载位图后出现此错误AsyncTask

我究竟做错了什么?

代码:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View vi = null;
    ImageView image;
    if(convertView==null) {
        vi = inflater.inflate(R.layout.gall_item, null);
        Log.i("info","convertView==null");
    } else {
        vi = inflater.inflate(R.layout.gall_item, null);
        Log.i("info","else");
    }
    holder.position = position;
    image = (ImageView)vi.findViewById(R.id.img);
    TextView url_img = (TextView)vi.findViewById(R.id.url_img);
    TextView cer = (TextView)vi.findViewById(R.id.cer);
    TextView idi = (TextView)vi.findViewById(R.id.ids);
    url_img.setText(img2[position]);
    cer.setText(cer_ar[position]);
    idi.setText(id_ar[position]);
    new LoadImageT(holder).execute(image, img1[position],activity.getApplicationContext(),position);
    return vi;
}

class LoadImageT extends AsyncTask<Object, Void, Bitmap> {
    Context context;
    private ImageView imv;
    private String url;
    private Bitmap bitmap = null;
    private int posS;
    private ViewHolder mHolder;

    public LoadImageT(ViewHolder holder) {
        mHolder = holder;
    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        imv = (ImageView) params[0];
        url = (String) params[1];
        context = (Context)params[2];
        posS = (Integer) params[3];
        bitmap = imageLoader.getBitmaptrue(url,imv,context);
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (mHolder.position == posS) {
            imv.setVisibility(View.VISIBLE);
            imv.setImageBitmap(result);
        } else {
            int stub_id = R.drawable.noimage;
            imv.setImageResource(stub_id);
        }
    }
}
4

1 回答 1

0

您以错误的方式进行操作,首先,如果您的视图为空,则安迪向您建议,膨胀视图并设置标签,然后在标签不为空时使用此标签,但这只会影响您的列表视图效率。其次从适配器中删除您的异步任务代码,将其放入活动并使用该对象。请参阅此链接Gilles 很好地解释了如何以有效的方式进行多线程。

于 2012-09-15T12:06:07.207 回答