GridView自定义适配器仅加载一个图像按钮

时间:2013-05-09 07:18:36

标签: java android android-asynctask android-gridview

我已经设置了一个自定义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;
}

适配器在asynctask中加载10个图像URL并将其存储到urls数组中。

我正在使用universal image loader下载并将网址中的图片加载到图片按钮中作为背景,

然而,只有一个图像按钮获得背景,其余图像为空白。

1 个答案:

答案 0 :(得分: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;
}

希望它有所帮助...