簡體   English   中英

從自定義適配器調用AsyncTask

[英]Call to AsyncTask from custom adapter

我用自定義適配器創建了一個listview。 字段之一是顯示每個用戶的化身的圖像。 我必須從網址獲得那些圖像。

我創建了一個將圖像從URL轉換為位圖的類。

我認為這應該從異步任務完成。 問題是我不知道如何從自定義適配器調用此方法。

這是我的課:

private class obtAvatar2 extends AsyncTask<Void , Void, Bitmap>{
    Bitmap bm;

    @Override
    protected Bitmap doInBackground(Void... voids) {
        try {

            URL url = new URL("https://www.bellatores.cl/wp-content/uploads/2018/01/Avatar-Mujer.png");
            URLConnection con = url.openConnection();
            con.connect();
            InputStream is = con.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();

        }catch (IOException e){

        }

        return bm;
    }
}

這將返回一個位圖。 然后從我的自定義適配器,我需要將該位圖放入ImageView

例如,我正在嘗試:

ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
avatarView.setImageBitmap(new obtAvatar2().execute());

但是,這是錯誤的:(

有什么建議嗎?

我建議您使用Glide或Picasso庫,它們是android應用程序中使用最多的圖像庫:

要使用gradle導入到您的項目中:

畢加索

dependencies {
    compile 'com.squareup.picasso:picasso:2.5.1'
}

滑行

dependencies {
    compile 'com.github.bumptech.glide:glide:3.5.2'
}

用法:

畢加索

Picasso.with(myFragment)
    .load(url)
    .into(myImageView);

滑行

Glide.with(myFragment)
    .load(url)
    .into(myImageView);

希望這可以幫助

您可以使用GlidePicasso 因為這些庫對於在適配器中設置圖像非常有用(這里的視圖可重用)。

如果您仍然想使用asynctask,請檢查以下內容:

在適配器中,每次滾動都會導致新的網絡調用,可以使用保存位圖對象來避免這種情況。

您正在嘗試使用以下代碼獲取圖像:

ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
avatarView.setImageBitmap(new obtAvatar2().execute());

這將不能用作:

new obtAvatar2().execute()

它將在后台執行,並在onPostExucute()中返回響應。 結果是:

avatarView.setImageBitmap(null)

如果您想使用asytask,那么可能需要使代碼如下:

private class obtAvatar2 extends AsyncTask<Void, Void, Bitmap> {
        Bitmap bm;

        @Override
        protected Bitmap doInBackground(Void... voids) {
            try {

                URL url = new URL("https://www.bellatores.cl/wp-content/uploads/2018/01/Avatar-Mujer.png");
                URLConnection con = url.openConnection();
                con.connect();
                InputStream is = con.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();

            } catch (IOException e) {

            }

            return bm;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
                avatarView.setImageBitmap(bitmap);
                //set bitmap to imageview and save in local list, so in future no need to download
            }
        }

您可以在構造函數中傳遞ImageView的引用。

首先,您應該在自定義適配器中添加obtAvatar2異步任務。

我希望您在customadapter中使用ViewHolder,然后在getView()中,將值分配給Imageview之前,調用異步任務。 例如:

 public static class ViewHolder {

    public ImageView display_adImage;

}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    try {
        if (convertView == null) {

            vi = inflater.inflate(R.layout.test_layout, null);
            holder = new ViewHolder();

            holder.display_adImage = vi.findViewById(R.id.IvAdImage);


            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        ...


        Bitmap b =  new GetImageTask().execute().get();
    holder.display_adImage.setImageBitmap(b);
}
}

 private class obtAvatar2 extends AsyncTask<Void , Void, Bitmap>{
Bitmap bm;

@Override
protected Bitmap doInBackground(Void... voids) {
    try {

        URL url = new URL("https://www.bellatores.cl/wp-content/uploads/2018/01/Avatar-Mujer.png");
        URLConnection con = url.openConnection();
        con.connect();
        InputStream is = con.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

    }catch (IOException e){

    }

    return bm;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM