【问题标题】:Call startActivityResult in adapter and call onActivityResult in Fragment在adapter中调用startActivityResult,在Fragment中调用onActivityResult
【发布时间】:2019-07-08 15:02:12
【问题描述】:

在适配器类中,用户可以在单击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
        }
    }

有人有想法吗?

【问题讨论】:

标签: android android-fragments kotlin startactivityforresult


【解决方案1】:

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

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

如果你想在那里获得结果,你应该使用 Fragment 的上下文。

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

所以你应该将你的片段引用传递给适配器。

【讨论】:

  • 如果我通过 Fragment,我会在这一行得到错误grid.setAdapter(ImageListAdapter(getActivity(), listOfImagesPath))
  • @jguerinet 找到所需的片段 fragmentActivity
【解决方案2】:

@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))

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 2013-11-29
    • 2019-07-20
    • 2016-10-22
    • 1970-01-01
    • 2020-11-15
    • 2012-11-29
    • 2019-11-19
    相关资源
    最近更新 更多