我正试图在我的片段中创建照片列表视图 图片未加载到片段中。我使用listadapter更新列表 但图像未显示在ui
中我已经采用了片段和listview的标准示例。
找到下面的代码段
@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
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
我发现,mThumbCursor.moveToNext()返回false。因此,不会调用else部分,也不会调用mAdapter.swapCursor。
但仍然坚持为什么会发生这种情况。