繁体   English   中英

如何在适配器中使用后台任务?

[英]How can I use a background task within an adapter?

我有这个图像适配器,应该可以处理更新图像网格。 问题是我有一个BackgroundImageTask运行,它从URL提取位图。 在后台任务完成更新之前,getView方法将返回imageView。 如何强制后台任务在返回imageview之前完成?

public class ImageAdapter extends BaseAdapter {
private Context mContext;
Bitmap bitmap;
List<Post> postList;
ImageView imageView;
int pos;

public ImageAdapter(Context c, List<Post> p) {
    mContext = c;
    postList = p;
}
public int getCount() {
    return postList.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}



// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    pos=position;
    View convert = convertView;
    if (convert == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 320));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convert;
    }
    new BackgroundImageTask().execute();

    return imageView;
}

public View SetBit(Bitmap b){
    imageView.setImageBitmap(b);
    return imageView;
}

class BackgroundImageTask extends AsyncTask<Void, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(Void... params) {
        Bitmap bit1=bitmap;
        try {

           bit1 = BitmapFactory.decodeStream((InputStream)new URL(postList.get(pos).thumbnail).getContent());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bit1;
    }

    @Override
    protected void onPostExecute(Bitmap bit) {
        SetBit(bit);
    }
}

}

在您的情况下:

您想确定何时完成Task并启动setAdapter。

getStatus()检查AsyncTask是待处理,正在运行还是已完成。

尝试以下方法。

 LoadImageInBackground loadTask = new LoadImageInBackground();

if(loadTask.getStatus() == AsyncTask.Status.PENDING){
// My AsyncTask has not started yet
}
if(loadTask.getStatus() == AsyncTask.Status.RUNNING){
// My AsyncTask is currently doing work in doInBackground()
}

if(loadTask.getStatus() == AsyncTask.Status.FINISHED){
// My AsyncTask is done and onPostExecute was called
}

相反,您可以将ImageView对象传递给后台任务,例如

    new BackgroundImageTask(imageView).execute();

  class BackgroundImageTask extends AsyncTask<Void, Void, Bitmap> {
  ImageView iv;
BackgroundImageTask(ImageView imageView){
 iv = imageView;
}

@Override
protected Bitmap doInBackground(Void... params) {
    Bitmap bit1=bitmap;
    try {

       bit1 = BitmapFactory.decodeStream((InputStream)new URL(postList.get(pos).thumbnail).getContent());

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bit1;
}

@Override
protected void onPostExecute(Bitmap bit) {
    iv.setImageBitmap(bit);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM