我写了一个活动,使用ImageLoaderConfiguration库将图像下载到Bitmap对象中。这工作正常。
我的下一个阶段是创建5个GalleryItem对象并将Bitmap放入这些对象中。
最后,我创建了一个GalleryItemAdapter对象,它扩展了ArrayAdapter。
问题是在函数getView中,getItem(position)
返回null。
以下是代码:
mainActivity:
package com.example.downloadimages;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import android.graphics.Bitmap;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
public String mUrl;
public Byte [] mImageBytes;
GalleryItem [] mGalleryItem;
Bitmap mBitmap;
public GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_view_in_layout);
mGridView = (GridView) findViewById(R.id.gridView);
mUrl = "http://www.royalcanin.in/var/royalcanin/storage/images/breeds/cat-breeds/norwegian-forest-cat/19311843-15-eng-GB/norwegian-forest-cat_cat_breed_cat_picture.jpg";
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .build();
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().loadImage(mUrl, new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mBitmap = loadedImage;
mGalleryItem = new GalleryItem[5];
for(GalleryItem gi : mGalleryItem)
{
gi = new GalleryItem();
gi.setBitmap(mBitmap);
}
setupAdapter();
}
});
}
void setupAdapter()
{
if ( mGridView == null) return;
mGridView.setAdapter(new GalleryItemAdapter(mGalleryItem));
}
private class GalleryItemAdapter extends ArrayAdapter<GalleryItem>
{
public GalleryItemAdapter(GalleryItem[] mGalleryItem)
{
super(getApplicationContext(), 0, mGalleryItem);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = (ImageView)findViewById(R.id.imageView1);
GalleryItem item = getItem(position);
((ImageView) convertView).setImageBitmap(item.getBitmap());
return convertView;
}
}
}
类GalleryItem:
public class GalleryItem {
Bitmap bitmap;
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
image_view_in_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout
谢谢!
答案 0 :(得分:0)
您的适配器代码错误。您正在尝试将位图设置为活动xml中的ImageView。您需要在适配器中创建新的ImageView并将其返回。你可以在这里查看https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView