0

我已经设置了一个自定义的 gridView 适配器,它看起来像这样

public class ImageAdapter extends BaseAdapter {

String[] urls = new String[10];
//....some code

class MyTask extends AsyncTask<Void,Void,Void>{

    @Override
    protected Void doInBackground(Void... arg0) {
           //perform a get request and fill up the "urls" array by 10 values..
     return null;
    }
}

public View getView(int position, View convertView, ViewGroup parent) {

    new MyTask().execute(); //execute AsyncTask

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridView = null;


    if (convertView == null) {
        gridView = new View(context);

      // get layout from grid_layout.xml
        gridView = inflater.inflate(R.layout.grid_layout, null);

        final ImageButton grid_art = (ImageButton) gridView.findViewById(R.id.grid_art);

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
        imageloader = ImageLoader.getInstance();
        imageloader.init(config);
        imageloader.loadImage(blog_urls[position], new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                grid_art.setImageBitmap(loadedImage);

                }
            });


    } else {


        gridView = (View) convertView;

    }

    return gridView;
}

@Override
public int getCount() {
    return urls.length;

}

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

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

适配器在异步任务中加载 10 个图像 url,并将其存储到 urls 数组中。

我正在使用通用图像加载器下载并加载在 urls 中找到的图像到 imagebutton 作为背景,

但是只有一个图像按钮获得背景,其余的都是空白的。

4

1 回答 1

0

像这样改变你的getView方法:

    public View getView(int position, View convertView, ViewGroup parent) {

    new MyTask().execute(); // execute AsyncTask

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //View gridView = null;

    MyView myView;
    if (convertView == null) {

        myView= new MyView();
        //gridView = new View(context);

        // get layout from grid_layout.xml
        convertView = inflater.inflate(R.layout.grid_layout, null);

        myView.grid_art = (ImageButton) convertView.findViewById(R.id.grid_art);
        convertView.setTag(myView);

    } else {
        myView= (MyView) convertView.getTag();
        //gridView = (View) convertView;
    }

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            context).build();
    imageloader = ImageLoader.getInstance();
    imageloader.init(config);

    imageloader.loadImage(blog_urls[position],
            new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view,
                        Bitmap loadedImage) {

                    myView.grid_art.setImageBitmap(loadedImage);

                }
            });

    return convertView;
}

在哪里MyView

 class MyView {
    ImageButton grid_art;
}

希望能帮助到你...

于 2013-05-09T07:32:04.890 回答