Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在viewModelScope中设置后,LiveData值为null_Android_Android Livedata_Android Architecture Components_Kotlin Coroutines_Android Architecture Lifecycle - Fatal编程技术网

Android 在viewModelScope中设置后,LiveData值为null

Android 在viewModelScope中设置后,LiveData值为null,android,android-livedata,android-architecture-components,kotlin-coroutines,android-architecture-lifecycle,Android,Android Livedata,Android Architecture Components,Kotlin Coroutines,Android Architecture Lifecycle,我有一个带有搜索功能的片段视图模型。虽然对象列表反映在我的RecyclerView上,但我使用协程从API获取,然后用结果设置MediatorLiveData值。当我尝试使用liveData.value访问MediatorLiveData值时,它返回null。我尝试过调试它,但是尽管显示了对象列表,我还是无法真正访问ViewModel中livedata的值 视图模型 class SearchViewModel@Inject构造函数(私有val mutwitsRepo:mutwitsRepo):B

我有一个带有搜索功能的片段视图模型。虽然对象列表反映在我的RecyclerView上,但我使用协程从API获取,然后用结果设置MediatorLiveData值。当我尝试使用
liveData.value
访问MediatorLiveData值时,它返回null。我尝试过调试它,但是尽管显示了对象列表,我还是无法真正访问ViewModel中livedata的值

视图模型
class SearchViewModel@Inject构造函数(私有val mutwitsRepo:mutwitsRepo):BaseViewModel(){
val query=MutableLiveData()
private val_isLoading=MutableLiveData()
val isLoading:LiveData=\u isLoading
private val_users=MediatorLiveData()
val用户:LiveData=\u用户
私有变量queryTextChangedJob:作业?=null
初始化{
_users.addSource(查询){queryStr->
if(queryStr.isNullOrEmpty())\u users.value=emptyList()
else searchUsers(queryStr)
}
}
趣味搜索用户(查询:字符串){
_isLoading.value=true
queryTextChangedJob?.cancel()
queryTextChangedJob=viewModelScope.launch{
延迟(300)
_users.value=mutwitsRepo.searchUsers(查询)
_isLoading.value=false
}
}
fun selectUser(用户:用户){
val temp=_users.value
temp?.find{it.id\u str==user.id\u str}?.toggleSelected()
_users.value=temp
}
覆盖有趣的onCleared(){
super.onCleared()
queryTextChangedJob?.cancel()
}
}
储存功能
suspend-fun-searchUsers(查询:字符串):List=withContext(Dispatchers.IO){
return@withContexttwitterService.searchUsers(查询)
我的代码如上所示,我已经被困了好几天了,任何帮助都将不胜感激!
}

显然,此问题是由于在我的适配器中注入了
ViewModel
造成的。我用dagger将viewmodel插入适配器,并使用函数
selectUser()
设置按钮的
onClickListener
。我将
clickListener
的设置提取到我的片段中,它成功了

显然,这个问题是由于在我的适配器中注入了
ViewModel
造成的。我用dagger将viewmodel插入适配器,并使用函数
selectUser()
设置按钮的
onClickListener
。我将
clickListener
的设置提取到我的片段中,它成功了

您是否尝试过使用“post”设置值而不是使用值设置值?是的,结果相同。我认为这个值没有问题,因为我在ViewModelScope中添加你的
活动/片段
代码,从那里你可以访问
LiveData
@Md.Asaduzzaman很抱歉回复太晚,我在一个片段中只有一个观察者,用于用
用户
LiveData填充recyclerview,我可以填充recyclerview,我只是不知道为什么我不能访问
用户
\u用户
的值。您是否尝试过用“post”来设置值,而不是用值来设置值?是的,相同的结果。我认为这个值没有问题,因为我在ViewModelScope中添加你的
活动/片段
代码,从那里你可以访问
LiveData
@Md.Asaduzzaman很抱歉回复太晚,我在一个片段中只有一个观察者,用于用
用户
LiveData填充recyclerview,我可以填充recyclerview,我只是不知道为什么我不能访问
用户
\u用户
class SearchViewModel @Inject constructor(private val mutwitsRepo: MutwitsRepo) : BaseViewModel() {

  val query = MutableLiveData<String>()

  private val _isLoading = MutableLiveData<Boolean>()
  val isLoading: LiveData<Boolean> = _isLoading

  private val _users = MediatorLiveData<List<User>>()
  val users: LiveData<List<User>> = _users

  private var queryTextChangedJob: Job? = null

  init {
    _users.addSource(query) { queryStr ->
      if (queryStr.isNullOrEmpty()) _users.value = emptyList()
      else searchUsers(queryStr)
    }
  }

  fun searchUsers(query: String) {
    _isLoading.value = true
    queryTextChangedJob?.cancel()
    queryTextChangedJob = viewModelScope.launch {
      delay(300)
      _users.value = mutwitsRepo.searchUsers(query)
      _isLoading.value = false
    }
  }

  fun selectUser(user: User) {
    val temp = _users.value
    temp?.find { it.id_str == user.id_str }?.toggleSelected()
    _users.value = temp
  }

  override fun onCleared() {
    super.onCleared()
    queryTextChangedJob?.cancel()
  }

}
suspend fun searchUsers(query: String): List<User> = withContext(Dispatchers.IO) {
  return@withContext twitterService.searchUsers(query)