GridView无法使用通用图像加载器加载

时间:2014-11-01 08:05:11

标签: java android image gridview universal-image-loader

我想在我的一个项目中使用UIL,所以我只是创建了这个原型项目来查看库的工作原理。我是根据https://github.com/nostra13/Android-Universal-Image-Loader的示例项目构建的。但是当我运行proj时,图像没有显示,logcat也没有显示任何错误。我只是在标题栏中看到一个带有项目名称的空白屏幕。任何人都可以告诉我代码中的错误。感谢任何帮助。

public class GridGallery extends Activity {
DisplayImageOptions options;
GridView listView;
String[] imageUrls = new String[] { "drawable://" + R.drawable.img1,
        "drawable://" + R.drawable.img2, "drawable://" + R.drawable.img3,
        "drawable://" + R.drawable.img4, "drawable://" + R.drawable.img5,
        "drawable://" + R.drawable.img6, "drawable://" + R.drawable.img7,
        "drawable://" + R.drawable.img8, "drawable://" + R.drawable.img9,
        "drawable://" + R.drawable.img10, "drawable://" + R.drawable.img11 };
public static final int INDEX = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            this).build();
    ImageLoader.getInstance().init(config);
    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
            .cacheOnDisk(true).considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565).build();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.image_grid, container, false);
    listView = (GridView) rootView.findViewById(R.id.grid);
    ((GridView) listView).setAdapter(new ImageAdapter());
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            startImagePagerActivity(position);
        }
    });
    return rootView;

}

protected void startImagePagerActivity(int position) {

    // Intent intent = new Intent(getActivity(), SimpleImageActivity.class);
    // intent.putExtra(Constants.Extra.FRAGMENT_INDEX,
    // ImagePagerFragment.INDEX);
    // intent.putExtra(Constants.Extra.IMAGE_POSITION, position);
    // startActivity(intent);
}

public class ImageAdapter extends BaseAdapter {

    private LayoutInflater inflater;

    ImageAdapter() {
        inflater = LayoutInflater.from(getApplicationContext());
    }

    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        View view = convertView;
        if (view == null) {
            view = inflater
                    .inflate(R.layout.image_grid_item, parent, false);
            holder = new ViewHolder();
            assert view != null;
            holder.imageView = (ImageView) view.findViewById(R.id.image);
            holder.progressBar = (ProgressBar) view
                    .findViewById(R.id.progress);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        ImageLoader.getInstance().displayImage(imageUrls[position],
                holder.imageView, options,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        holder.progressBar.setProgress(0);
                        holder.progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,
                            FailReason failReason) {
                        holder.progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                            View view, Bitmap loadedImage) {
                        holder.progressBar.setVisibility(View.GONE);
                    }
                }, new ImageLoadingProgressListener() {
                    @Override
                    public void onProgressUpdate(String imageUri,
                            View view, int current, int total) {
                        holder.progressBar.setProgress(Math.round(100.0f
                                * current / total));
                    }
                });

        return view;
    }

}

static class ViewHolder {
    ImageView imageView;
    ProgressBar progressBar;
}
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imagetester"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".GridGallery"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

因为您忘记将onCreateView中的项目添加到网格视图的适配器中。您必须在设置gridview的适配器后手动添加项目。