CursorAdapterのgetItem(position)で任意のpositionのcursorを取得する方法が分かりません。
ContentResolverから端末内の画像データを取得してCursorAdapterにcursorを渡しました。
kotlin
1 fun loadImage() { 2 loaderManager.initLoader(0, null, object : LoaderManager.LoaderCallbacks<Cursor> { 3 override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> { 4 val queryUri = MediaStore.Files.getContentUri("external") 5 val selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE 6 val cursorLoader = CursorLoader( 7 context, 8 queryUri, 9 null, 10 selection, 11 null, 12 MediaStore.Files.FileColumns.DATE_ADDED 13 ) 14 return cursorLoader 15 } 16 17 override fun onLoadFinished(loader: Loader<Cursor>?, data: Cursor) { 18 imageCursorAdapter = ImageCursorAdapter(context, data, 0) 19 listAdapter = imageCursorAdapter 20 } 21 override fun onLoaderReset(loader: Loader<Cursor>?) { 22 23 } 24 }) 25 }
adapter
kotlin
1 class ImageCursorAdapter(val context: Context?, c: Cursor?, flags: Int) : CursorAdapter(context, c, flags) { 2 3 private val choiceList: HashSet<Cursor> 4 5 init { 6 this.choiceList = HashSet<Cursor>() 7 } 8 9 override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup?): View? { 10 //省略 11 } 12 13 override fun bindView(view: View?, context: Context?, cursor: Cursor?) { 14 //省略 15 16 Log.d("テスト", "ゲット1" + getItem(1).toString()) 17 Log.d("テスト", "ゲット2" + getItem(2).toString()) 18 Log.d("テスト", "ゲット3" + getItem(3).toString()) 19 } 20 21 override fun getItem(position: Int): Cursor { 22 return super.getItem(position) as Cursor 23 } 24 25 26 fun choice(position: Int) { 27 val item = getItem(position) 28 if (choiceList.contains(item)) { 29 choiceList.remove(item) 30 } else { 31 choiceList.add(item) 32 } 33 notifyDataSetChanged() 34 } 35 36} 37
bindView()で任意の値を代入してログを見てみたのですが全て同じ値でした。
テスト: ゲット1android.content.ContentResolver$CursorWrapperInner@3cb38fd
テスト: ゲット2android.content.ContentResolver$CursorWrapperInner@3cb38fd
テスト: ゲット3android.content.ContentResolver$CursorWrapperInner@3cb38fd
任意のpositionのcursorのデータ(レコード?)を取得したい場合どのようにすればよいか教えてください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/06/02 08:27
2017/06/02 09:00
退会済みユーザー
2017/06/02 11:50
2017/06/02 12:07 編集
退会済みユーザー
2017/06/02 12:18