在adapter中调用startActivityResult,在Fragment中调用onActivityResult
Call startActivityResult in adapter and call onActivityResult in Fragment
在适配器 class 中,用户可以在单击 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
}
}
有人有想法吗?
我认为问题在于您在 activity 的上下文中调用了 startActivityForResult。
private val context: FragmentActivity?,
context?.startActivityForResult(captureIntent, 1);
如果您想在那里获得结果,您应该改用片段的上下文..
private val fragment: Fragment?,
fragment?.startActivityForResult(captureIntent, 1);
因此您应该将片段引用传递给适配器。
@sinek 是对的,您需要从 Fragment
实例而不是 activity 调用 startActivityForResult()
。这需要将 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))
在适配器 class 中,用户可以在单击 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
}
}
有人有想法吗?
我认为问题在于您在 activity 的上下文中调用了 startActivityForResult。
private val context: FragmentActivity?,
context?.startActivityForResult(captureIntent, 1);
如果您想在那里获得结果,您应该改用片段的上下文..
private val fragment: Fragment?,
fragment?.startActivityForResult(captureIntent, 1);
因此您应该将片段引用传递给适配器。
@sinek 是对的,您需要从 Fragment
实例而不是 activity 调用 startActivityForResult()
。这需要将 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))