0

I m trying to create a listview of photos in my fragment
images are not loaded in the fragment. I m updating the list using the listadapter
but the images are not shown in the ui

I have taken standard example of fragments and listview.

Find the snippet below

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Create a new TextView and set its text to the fragment's section
    // number argument value.
    View view = inflater.inflate(R.layout.photos_list_main, container,
            false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.e("PhotosListFragment", "On Activity Created ");

    /** Initializing MatrixCursor */
    mMatrixCursor = new MatrixCursor(new String[] { "_id", "_data",
            "_details" });

    /** Getting a reference to listview of the MainActivity layout */
    ListView lVThumbnail = (ListView) this.getActivity().findViewById(
            R.id.lv_photos);

    /** Creating an adapter object to set image and text in the listview */
    mAdapter = new SimpleCursorAdapter(this.getActivity()
            .getApplicationContext(), R.layout.photos_listview, null,
            new String[] { "_data", "_details" }, new int[] {
                    R.id.img_thumbnail, R.id.tv_details }, 0);

    lVThumbnail.setAdapter(mAdapter);

    lVThumbnail.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Log.e("PhotosListFragment", "item clicked " + arg2);
        }

    });
    /** Loader to get thumbnail images from the SD Card */
    this.getLoaderManager().initLoader(THUMBNAIL_LOADER_ID, null, this);


}

@Override
public Loader<Cursor> onCreateLoader(int loader_id, Bundle arg1) {

    CursorLoader cLoader;
    Uri uri;

    if (loader_id == THUMBNAIL_LOADER_ID) {
        /** Querying Thumbnail content provider */
        uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
        cLoader = new CursorLoader(this.getActivity(), uri, null, null,
                null, null);
    } else {
        /** Querying Image Content provider with thumbnail image id */
        String image_id = arg1.getString("image_id");
        uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        cLoader = new CursorLoader(this.getActivity(), uri, null,
                MediaStore.Images.Media._ID + "=" + image_id, null, null);
    }
    return cLoader;

}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    if (arg0.getId() == THUMBNAIL_LOADER_ID) {
        /** Thumbnail cursor is loaded completely */
        mThumbCursor = arg1;

        if (mThumbCursor.moveToFirst()) {
            /** Taking the first thumbnail */

            mThumbImageId = mThumbCursor.getString(mThumbCursor
                    .getColumnIndex(MediaStore.Images.Thumbnails._ID));
            mThumbImageData = mThumbCursor.getString(mThumbCursor
                    .getColumnIndex(MediaStore.Images.Thumbnails.DATA));

            /**
             * Getting the image id from the mThumbCursor and putting in to
             * the bundle
             */
            String image_id = mThumbCursor.getString(mThumbCursor
                    .getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID));
            Bundle data = new Bundle();
            data.putString("image_id", image_id);

            /** Intiates the Image Loader onCreateLoader() */
            getLoaderManager().initLoader(IMAGE_LOADER_ID, data, this);


        }

    } else if (arg0.getId() == IMAGE_LOADER_ID) {
        mImageCursor = arg1;

        if (mImageCursor.moveToFirst()) {

            mImageTitle = mImageCursor.getString(mImageCursor
                    .getColumnIndex(MediaStore.Images.Media.TITLE));
            mImageSize = mImageCursor.getString(mImageCursor
                    .getColumnIndex(MediaStore.Images.Media.SIZE));
            mImageWidth = mThumbCursor.getString(mThumbCursor
                    .getColumnIndex(MediaStore.Images.Thumbnails.WIDTH));
            mImageHeight = mThumbCursor.getString(mThumbCursor
                    .getColumnIndex(MediaStore.Images.Thumbnails.HEIGHT));

            String details = "Title:" + mImageTitle + "\n" + "Size:"
                    + mImageSize + " Bytes " + "\n" + "Resolution:"
                    + mImageWidth + " x " + mImageHeight;

            /** Adding new row to the matrixcursor object */
            mMatrixCursor.addRow(new Object[] { mThumbImageId,
                    mThumbImageData, details });

            /** Taking the next thumbnail */
            if (mThumbCursor.moveToNext()) {

                mThumbImageId = mThumbCursor.getString(mThumbCursor
                        .getColumnIndex(MediaStore.Images.Thumbnails._ID));
                mThumbImageData = mThumbCursor.getString(mThumbCursor
                        .getColumnIndex(MediaStore.Images.Thumbnails.DATA));

                String image_id = mThumbCursor
                        .getString(mThumbCursor
                                .getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID));
                Bundle data = new Bundle();
                data.putString("image_id", image_id);

                /**
                 * Restarting the image loader to get the next image details
                 */
                getLoaderManager().restartLoader(IMAGE_LOADER_ID, data,
                        this);

            } else {
                /** No more thumbnails exists */
                if (mThumbCursor.isAfterLast())
                    mAdapter.swapCursor(mMatrixCursor);
                /** Set the thumbnails and its details in the listview */
            }
        }
    }

}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub

}
4

2 回答 2

1

发现错误,我需要添加和 else case 语句并调用

mAdapter.swapCursor(mMatrixCursor);

else {
            /** No more thumbnails exists */
            if (mThumbCursor.isAfterLast())
                mAdapter.swapCursor(mMatrixCursor);
            /** Set the thumbnails and its details in the listview */
        }
}
else
{
     mAdapter.swapCursor(mMatrixCursor);
}
于 2013-06-04T06:27:19.037 回答
0

我发现 mThumbCursor.moveToNext() 返回错误。因此不调用 else 部分并且不调用 mAdapter.swapCursor。

但仍然坚持为什么会发生这种情况。

于 2013-06-04T05:59:58.207 回答