在自定义listvew Android上实现通用图像加载器

时间:2016-04-14 04:21:04

标签: android listview universal-image-loader

我该如何实现?我一直在寻找,我迷路了。如果有人能够启发我会很棒。这是我当前的自定义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;
  };
}

请注意,上面的代码有一些错误。首先,在activity.getApplicationContext():我不知道为什么。对于第二个,imageLoader表示它无法解析符号。

3 个答案:

答案 0 :(得分:1)

请检查以下代码

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

不要忘记在应用程序类中初始化ImageLoader。

答案 1 :(得分:0)

好吧我解决了我的问题,也许我应该在这里为以后的人分享:)

首先我必须初始化主要活动中的ImageLoader(不在customlistadapter中),如下所示:

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

接下来我从customlistadapter调用ImageLoader:

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

和ImageLoader的声明:

public static ImageLoader imageLoader;

我不知道如果声明为public和static会不会引起任何问题,但是嘿它有效。

答案 2 :(得分:0)

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