我有一个片段活动,onCreate
方法包含以下代码;
newListView = (ListView) findViewById(R.id.new_listView);
Intent intent = getIntent();
id = intent.getStringExtra("id");
databaseAdapter = new DataBaseAdapter(getApplicationContext());
databaseAdapter = databaseAdapter.open();
String[] new_list = databaseAdapter.getNewByID(id);
NewAdapter madapter = new NewAdapter(getApplicationContext(), new_list);
newListView.setAdapter(madapter);
databaseAdapter 工作正常。(当我调试显示图片网址时)。 new_list
包含图片网址列表。
NewAdapter 类如下所示;
public class NewAdapter extends ArrayAdapter<String>{
String [] images;
LayoutInflater inflater;
Context context;
private ImageLoader imageLoader;
public NewAdapter(Context context,String [] images) {
super(context,R.layout.new_cell);
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.new_cell, null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.new_imageView);
imageLoader.displayImage(images[position],image);
return convertView;
}
当我运行代码并打开活动时,图像未显示。请帮帮我。
答案 0 :(得分:0)
只需在Adapter构造函数中添加一个新行。
public NewAdapter(Context context,String [] images) {
super(context,R.layout.new_cell);
this.images = images;
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
//initImageLoader(context);
}
自定义初始化在使用ImageLoader之前使用以下调用一次。
public static void initImageLoader(Context context) {
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher).resetViewBeforeLoading()
.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
// END - UNIVERSAL IMAGE LOADER SETUP
}