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

时间:2017-03-29 02:23:27

标签: java android multithreading background task

我有这个图像适配器,应该处理更新图像网格。问题是我有一个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);
    }
}

}

2 个答案:

答案 0 :(得分:0)

在你的情况下:

您希望确保完成任务并启动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
}

答案 1 :(得分:0)

相反,您可以将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);
}
}