在适配器中调用StartActivityResult,并在碎片中调用onActivityResult



在适配器类中,当单击holder.image时,用户可以捕获图像。我已经将onActivityResult设置为片段,但没有被调用。

fragment

 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")
        }
    }

ADAPTER

 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
        }
    }

有人有想法吗?

我认为问题在于您在活动的上下文上调用StartActivityForresult。

 private val context: FragmentActivity?,
 context?.startActivityForResult(captureIntent, 1);

如果要在那里获得结果,则应使用片段的上下文。

 private val fragment: Fragment?,
 fragment?.startActivityForResult(captureIntent, 1);

因此,您应该将片段引用到适配器。

@sinek是正确的,您需要从Fragment实例调用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))

最新更新