Ardi Sugiarto
Ardi Sugiarto

Reputation: 95

Implement universal image loader on custom listvew Android

How do I implement this? I've been looking everywhere and I'm lost. If someone could enlighten me that would be great. This is my current custom adapter:

public class CustomListAdapter extends ArrayAdapter<String> 
{
private final Activity context;
private final String[] itemname;
private final String[] imageUrls;

public CustomListAdapter(Activity context, String[] itemname, String[] imageUrls) {
    super(context, R.layout.mylist, itemname);
    this.context = context;
    this.itemname = itemname;
    this.imageUrls = imageUrls;
    //ImageLoader imageLoader = new ImageLoader(activity.getApplicationContext());
}

public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist, null,true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
    txtTitle.setText(itemname[position]);
    //imageLoader.displayImage(imageUrls[position], imageView, null);
    extratxt.setText("Description "+itemname[position]);
    return rowView;
  };
}

Note that the above code has some errors. First, at the activity.getApplicationContext(): I don't know why. For the second one, the imageLoader says it cannot resolve symbol.

Upvotes: 1

Views: 216

Answers (3)

Saurabh sharma
Saurabh sharma

Reputation: 379

Try implementing the below code in your adapter 

         ImageLoader imageLoader = ImageLoader.getInstance();

        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        DisplayImageOptions option = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(1000)).cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher)
                .showImageOnLoading(R.drawable.ic_launcher).bitmapConfig(Bitmap.Config.RGB_565).build();


        imageLoader.displayImage(imageurl,imageview,option);

Upvotes: 0

Ardi Sugiarto
Ardi Sugiarto

Reputation: 95

okay I resolved my problem, maybe I should share it here for people in the future :)

first I have to initialize the ImageLoader in main activity(not in the customlistadapter) like this:

imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));

next I call the ImageLoader from the customlistadapter:

MainActivity.imageLoader.displayImage(imageUrls[position],imageView);

and the declaration of the ImageLoader :

public static ImageLoader imageLoader;

I don't know if it would cause any problem if declared public and static, but hey it works.

Upvotes: 0

Sanjay Kakadiya
Sanjay Kakadiya

Reputation: 1636

Please check below code

public class CustomListAdapter extends ArrayAdapter<String> 
{
private final Activity context;
private final String[] itemname;
private final String[] imageUrls;
ImageLoader imageLoader;
public CustomListAdapter(Activity context, String[] itemname, String[] imageUrls) {
    super(context, R.layout.mylist, itemname);

    this.context = context;
    this.itemname = itemname;
    this.imageUrls = imageUrls;
    imageLoader = new ImageLoader(context);
}

public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

    txtTitle.setText(itemname[position]);
    imageLoader.displayImage(imageUrls[position], imageView, null);
    extratxt.setText("Description "+itemname[position]);
    return rowView;
  }
}

don't forget initialization ImageLoader in application class.

Upvotes: 1

Related Questions