2013-06-03 100 views
0

我試圖在片段中創建照片的列表視圖
圖像未加載到片段中。我更新列表使用列表適配器
但圖像不顯示在UI在片段中的列表視圖不能正常工作

我已經採取了片段和列表視圖的標準示例。

查找下面

@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 

} 

回答

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); 
} 
0

片斷我想通了,mThumbCursor.moveToNext()方法返回false。因此else部分未被調用,並且mAdapter.swapCursor未被調用。

但仍然堅持爲什麼會發生這種情況。