简体   繁体   English

无法通过 onDateSet 方法为视图模型中的 livedata 赋值

[英]Cant assign value to livedata in viewmodel from onDateSet method

i cant update the value of dateTxt and timeTxt, it is always null. i tried calling setDate from a fragment and it works.我无法更新 dateTxt 和 timeTxt 的值,它始终为 null。我尝试从一个片段调用 setDate,它起作用了。 And the value date Mutablelive data remains null. When i call function from onDateset it actually come inside viewModel and the value never updates.并且值日期 Mutablelive 数据仍然是 null。当我从 onDateset 调用 function 时,它实际上进入 viewModel 并且该值永远不会更新。

My viewModel我的视图模型

 class OrderViewModel @ViewModelInject constructor(
    private val networkHelper: NetworkHelper,
    private val firebaseRepository: FirebaseRepository

) : ViewModel(){

  val date:MutableLiveData<Date> = MutableLiveData()
    val dateTxt:MutableLiveData<String> = MutableLiveData()
    val timeTxt:MutableLiveData<String> = MutableLiveData("Set Time")

fun setDate(year:Int?, month:Int?, day:Int?,hour:Int?, minute:Int?){

        viewModelScope.launch {
            if (networkHelper.isNetworkConnected()){
                date.value = Date(year,month,day,hour,minute)

                if(year!=null && month!=null && day!=null){
                    val c = Calendar.getInstance()
                    c.set(Calendar.YEAR,year)
                    c.set(Calendar.MONTH,month)
                    c.set(Calendar.DAY_OF_MONTH,day)
                    val dateString= DateFormat.getDateInstance(DateFormat.FULL).format(c.time).toString()
                    dateTxt.value = dateString
                }

                if (hour!=null && minute!=null){
                    val c = Calendar.getInstance()
                    c.set(Calendar.HOUR,hour)
                    c.set(Calendar.MINUTE,minute)
                    timeTxt.value = DateFormat.getDateInstance(DateFormat.SHORT).format(c.time).toString()

                }
            }
        }
    }
}

DatePickerDailog DatePickerDailog

@AndroidEntryPoint
class DatePickerDialog :DialogFragment(), DatePickerDialog.OnDateSetListener{

    private val viewmodel:OrderViewModel by viewModels()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)
        return DatePickerDialog(requireContext(),this,year,month,day)
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {

        viewmodel.setDate(year,month,dayOfMonth,null,null)

    }


}

Solved解决了

I actually created two instances of viewmodel, to fix that i used shared viewModel, like我实际上创建了两个 viewmodel 实例,以修复我使用共享 viewModel 的问题,例如

private val viewmodel:OrderViewModel by activityViewModels() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 viewModel 中测试从存储库返回 LiveData 值的方法 - How to test a method in viewModel which returns LiveData value from repository 我们如何在ViewModel中将LiveData从Room“分配”到MutableLiveData - How can we “assign” LiveData from Room to MutableLiveData, within ViewModel 从 ViewModel 观察 LiveData - Observing LiveData from ViewModel 提供一个观察 viewModel 的活动,该活动是否可以直接为 viewModel livedata 赋值 - Giving an activity that observes a viewModel, can that activity assign value directlty to viewModel livedata 无法从 ViewModel 观察 Livedata - Unable to observe Livedata from ViewModel ViewModel从另一个ViewModel观察LiveData - ViewModel observes LiveData from another ViewModel DatePicker onDateSet 方法未运行 - DatePicker onDateSet method not running 在应用进程被杀死之前,如何在 ViewModel 的 onCleared 方法中为 SavedStateHandle 提供的 LiveData 设置特定值 - How to set a specific value to a LiveData provided by SavedStateHandle in onCleared method of ViewModel before the app process is killed 如何将值从Button.setOnClickListener传递给Android onDateSet - How to pass value from Button.setOnClickListener to onDateSet Android 如何将 editText 值传递给 viewModel 和 Livedata (Kotlin) - How to pass editText value to viewModel and Livedata (Kotlin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM