在适配器类中,用户可以在单击holder.image
时捕获图像。我在Fragment中设置了onActivityResult
,但是没有被调用。
片段
grid.setAdapter(ImageListAdapter(getActivity(), listOfImagesPath))
....
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
val mAdapter: ImageListAdapter?=null
mAdapter?.onActivityResult(requestCode, resultCode, data);
longToast("abc")
var bitmap: Bitmap? = null
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
longToast("hello")
} else {
longToast("bye")
}
}
适配器
class ImageListAdapter(
private val context: FragmentActivity?,
private val imgPics: List<String>?
) : BaseAdapter() {
override fun getItem(position: Int): Any {
return position
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
if (imgPics != null)
return imgPics.size
else
return 1;
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var holder: ItemHolder
var convertView = convertView
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.grid_item, null)
holder = ItemHolder()
holder.image = convertView!!.findViewById(R.id.imageView)
holder.image?.setImageResource(R.drawable.ic_add)
holder.image?.setColorFilter(R.color.colorGainsboro, PorterDuff.Mode.SRC_ATOP);
convertView.tag = holder
}
holder = convertView?.tag as ItemHolder
....
holder.image?.setImageBitmap(bm)
}
}
holder.image?.setOnClickListener{
dispatchTakePictureIntent()
}
return convertView
}
private fun dispatchTakePictureIntent() {
try {
val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
context?.startActivityForResult(captureIntent, 1);
} catch (e: ActivityNotFoundException) {
e.printStackTrace()
}
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
Log.e("MyAdapter", "onActivityResult")
}
internal class ItemHolder {
var image: ImageView? = null
}
}
有人有主意吗?
答案 0 :(得分:3)
我认为问题在于您在活动的上下文中调用startActivityForResult。
private val context: FragmentActivity?,
context?.startActivityForResult(captureIntent, 1);
如果要在此处获取结果,则应改用Fragment的上下文。
private val fragment: Fragment?,
fragment?.startActivityForResult(captureIntent, 1);
因此,您应该将片段引用传递给适配器。
答案 1 :(得分:1)
@sinek是正确的,您需要从startActivityForResult()
实例而不是活动中调用Fragment
。这需要将Fragment
实例传递到您的适配器,并修复旧的context
调用,结果是:
class ImageListAdapter(
private val fragment: Fragment,
private val imgPics: List<String>?
) : BaseAdapter() {
...
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var holder: ItemHolder
var convertView = convertView
if (convertView == null) {
// Here I am simply using the parent Context, as parent will never be null
// (Notice how I changed ViewGroup? to ViewGroup above)
// But if you prefer you can use fragment.context
convertView = LayoutInflater.from(parent.context).inflate(R.layout.grid_item, null)
...
}
...
}
private fun dispatchTakePictureIntent() {
try {
val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fragment.startActivityForResult(captureIntent, 1);
} catch (e: ActivityNotFoundException) {
e.printStackTrace()
}
}
...
}
然后,在您的片段中:
grid.setAdapter(ImageListAdapter(this, listOfImagesPath))