【问题标题】:How can I use a background task within an adapter?如何在适配器中使用后台任务?
【发布时间】:2017-03-29 02:23:27
【问题描述】:

我有这个图像适配器,它应该处理更新图像网格。问题是我正在运行一个从 URL 获取位图的 BackgroundImageTask。 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);
    }
}

}

【问题讨论】:

    标签: java android multithreading background task


    【解决方案1】:

    在你的情况下:

    您要确定 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
    }
    

    【讨论】:

    • 我会在哪里做这个?
    • 这取决于您使用 setadapter(ImageAdapter) 方法的位置。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多