如何将日期从datepickerfragment发送到另一个片段

8oomwypt  于 2021-09-29  发布在  Java
关注(0)|答案(3)|浏览(316)

我是android开发新手。通过big nerd ranch android 4e book学习这是一个例子,书中的方法被折旧了,那么我如何将日期数据发送到另一个片段
他们使用的是贬值的targetfragment技术,所以我现在被困在这个问题上
这是我的 datepickerfragment.kt 文件:

private const val ARG_DATE = "date"
private const val RESULT_DATE_KEY = "resultDate"
private const val ARG_REQUEST_CODE = "requestCode"

class DatePickerFragment : DialogFragment() {

   override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

       val dateListener = DatePickerDialog.OnDateSetListener { _: DatePicker, year: Int, month: Int, day: Int ->
           val resultDate: Date = GregorianCalendar(year, month, day).time

           val result = Bundle().apply {
               putSerializable(RESULT_DATE_KEY, resultDate)

           }

           val resultRequestCode = requireArguments().getString(ARG_REQUEST_CODE, "")
       }

       val date = arguments?.getSerializable(ARG_DATE) as Date
       val calendar = Calendar.getInstance()
       calendar.time = date
       val initialYear = calendar.get(Calendar.YEAR)
       val initialMonth = calendar.get(Calendar.MONTH)
       val initialDay = calendar.get(Calendar.DAY_OF_MONTH)

       return DatePickerDialog(
               requireContext(),
               dateListener,
               initialYear,
               initialMonth,
               initialDay
       )
   }

   interface Callbacks {
       fun onDateSelected(date: Date)
   }

   companion object {

       fun getSelectedDate(result: Bundle) = result.getSerializable(RESULT_DATE_KEY) as Date

       fun newInstance(date: Date, requestCode: String): DatePickerFragment {
           val args = Bundle().apply {
               putSerializable(ARG_DATE, date)
               putString(ARG_REQUEST_CODE, requestCode)
           }

           return DatePickerFragment().apply {
               arguments = args
           }

       }
   }

}

这是我的crimefragment.kt课程:

private const val ARG_CRIME_ID = "crime_id"
private const val TAG = "CrimeFragment"
private const val REQUEST_DATE = "DialogDate"

class CrimeFragment : Fragment(), DatePickerFragment.Callbacks, FragmentResultListener {

    private lateinit var crime: Crime
    private lateinit var titleField: EditText
    private lateinit var dateButton: Button
    private lateinit var solvedCheckBox: CheckBox

    private val crimeDetailViewModel: CrimeDetailViewModel by lazy {
        ViewModelProvider(this).get(CrimeDetailViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        crime = Crime()
        val crimeId: UUID = arguments?.getSerializable(ARG_CRIME_ID) as UUID
        Log.d(TAG, "args bundle crime Id:$crimeId")

        crimeDetailViewModel.loadCrime(crimeId)
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_crime, container, false)

        titleField = view.findViewById(R.id.crime_title) as EditText
        solvedCheckBox = view.findViewById(R.id.crime_solved1) as CheckBox
        dateButton = view.findViewById(R.id.crime_date)

        return view

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        crimeDetailViewModel.crimeLiveData.observe(
                viewLifecycleOwner,
                Observer { crime ->
                    crime?.let {
                        this.crime = crime
                        updateUI()
                    }

                }
        )
        childFragmentManager.setFragmentResultListener(REQUEST_DATE, viewLifecycleOwner, this)

    }

    override fun onStart() {
        super.onStart()

        val titleWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                crime.title = s.toString()
            }

            override fun afterTextChanged(s: Editable?) {

            }

        }
        titleField.addTextChangedListener(titleWatcher)

        dateButton.setOnClickListener {
            DatePickerFragment
                    .newInstance(crime.date, REQUEST_DATE)
                    .show(childFragmentManager, REQUEST_DATE)

        }

        solvedCheckBox.apply {
            setOnCheckedChangeListener { _, isChecked ->
                crime.isSolved = isChecked
            }
        }
    }

    private fun updateUI() {
        titleField.setText(crime.title)
        dateButton.text = crime.date.toString()
        solvedCheckBox.apply {
            isChecked = crime.isSolved
            jumpDrawablesToCurrentState()

        }
    }

    companion object {
        fun newInstance(crimeId: UUID): CrimeFragment {
            val args = Bundle().apply {
                putSerializable(ARG_CRIME_ID, crimeId)

            }
            return CrimeFragment().apply { arguments = args }
        }
    }

    override fun onStop() {
        super.onStop()
        crimeDetailViewModel.saveCrime(crime)
    }

    override fun onDateSelected(date: Date) {
        crime.date = date
    }

    override fun onFragmentResult(requestCode: String, result: Bundle) {
        when (requestCode) {
            REQUEST_DATE -> {
                Log.d(TAG, "received result for $requestCode")
                crime.date = DatePickerFragment.getSelectedDate(result)
                updateUI()
            }
        }
    }

}
gojuced7

gojuced71#

您似乎正在使用片段结果api,这需要两个步骤。首先是你 setFragmentResultListener 在您希望接收结果的片段中(在您的案例中为crimefragment),在调用 setFragmentResult 从产生结果的片段。你错过了第二步。要解决此问题,请更新 OnDateSetListener 作为

val dateListener = DatePickerDialog.OnDateSetListener { _: DatePicker, year: Int, month: Int, day: Int ->
    val resultDate: Date = GregorianCalendar(year, month, day).time
    val result = Bundle().apply {
        putSerializable("DATE", resultDate)
    }
    // Set the fragment result, this will invoke the `onFragmentResult` of CrimeFragment
    parentFragmentManager.setFragmentResult("requestKey", result)
}

现在在 CrimeFragment 登记 FragmentResultListener 作为

// use same value of requestKey as specified in setFragmentResult
childFragmentManager.setFragmentResultListener("requestKey", viewLifecycleOwner, this)

现在更新 onFragmentResult

override fun onFragmentResult(requestKey: String, result: Bundle) {
     when(requestKey){
        "requestKey" -> {
            // get date from the result bundle
            val date = result.getString("DATE")
            // do something with date
        }
    }
}

除此之外,您还可以采取另一种方法,即使用共享 ViewModel ,因为这两个片段共享活动,所以您可以获得 ViewModel 与活动相关的。
获取crimefragment和datepickerfragment中的viewmodel作为

// Get view model associated with activity
private val crimeDetailViewModel: CrimeDetailViewModel by activityViewModels()

现在在 OnDateSetListener 属于 DatePickerFragment 只需将日期存储在某个viewmodel属性中即可

crimeDetailViewModel.date = // updated date from OnDateSetListener

在此之后,您可以访问 crimeDetailViewModel.date 在…内 CrimeFragment

chy5wohz

chy5wohz2#

如果使用jetpack组件中的导航组件,则可以使用 getPreviousBackStackEntry() 要从选择器对话框中用选择器的结果填充启动器片段的捆绑包,然后当返回到启动器片段时,使用 getCurrentBackStackEntry .
您可以参考此视频以获取示例
如果不使用导航组件,则可以使用作用域为活动的共享viewmodel。您将在两个片段中获得对该共享viewmodel的引用。然后,日期选择器将填充共享viewmodel中的某个字段,启动程序片段将在恢复时读取该字段。

vwkv1x7d

vwkv1x7d3#

一种方法是使用事件、总线事件或rx。

相关问题