2016-04-14 67 views
1

如何實現此目的?我一直在尋找,我迷路了。如果有人能夠啓發我,那會很棒。這是我目前定製adapter在自定義listvew上實現通用圖像加載器Android

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說它不能解析符號。

+0

按照以下鏈接https://github.com/nostra13/Android-Universal-Image-Loader http://javatechig.com/android/universal-image-loader-library-in提到的步驟-android – Sanjeev

回答

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的。

+0

activity.getApplicartionContext()仍然給我錯誤...以及如何進行初始化?這是我第一次使用通用圖像加載器,所以我在這裏損失。 –

+0

檢查已更新的答案。 –

0

好吧,我解決我的問題,也許我應該在這裏分享它在未來:)

首先我必須在主要活動中(而不是在customlistadapter)來初始化ImageLoader人是這樣的:

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

下一個我稱之爲從customlistadapter的ImageLoader的:

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

和decla在ImageLoader的配給:

public static ImageLoader imageLoader;

我不知道,如果聲明的公共和靜態它會導致任何問題,但嘿它的工作原理。

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); 
相關問題