0

I have setup a custom gridView adapter, which looks like this

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;
}

the adapter loads 10 image url's in an asynctask and stores it into the urls array.

I am using universal image loader to download and load images found at the urls into an imagebutton as background,

however only one imagebutton gets the background and the rest are blank.

user1910290
  • 537
  • 4
  • 15
  • 28

1 Answers1

0

Change your getView method like this:

    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;
}

Where MyView is:

 class MyView {
    ImageButton grid_art;
}

Hope it helps...

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25
  • two lint errors: grid_art cannot be resolved or is not a field on setimagebitmap(loadedimage), and gridView cannot be resolved to a variable on return gridView. – user1910290 May 09 '13 at 08:06
  • ok, so the asynctask is being called multiple times, I think calling it once is enough since it will load values into the urls array as it progresses, also when I scroll up and down of the gridview the position of the images change :o – user1910290 May 09 '13 at 08:24
  • yes calling the `AsyncTask` once will be enough, and then in the `onPostExecute` method set the adapter in `GridView` – AnujMathur_07 May 09 '13 at 08:46
  • asynctask code is written in the adapter class itself so I can set adapter there. – user1910290 May 09 '13 at 08:50
  • and also how do I fix the images changing position – user1910290 May 09 '13 at 08:51