我想从viewmodel类中的SharedReferences中获取一些信息。构造函数只有一个savedstatehandle参数,我不知道如何获取上下文。这是我的密码:
public class PropertyDetailActivityViewModel extends ViewModel{
public enum Event{
Ok, Ko
}
public MutableLiveData<Event> event = new MutableLiveData<>();
public MutableLiveData<Property> property = new MutableLiveData<>();
private final Property propertyExtra;
public PropertyDetailActivityViewModel(SavedStateHandle savedStateHandle) {
propertyExtra = savedStateHandle.get(PropertyDetailActivity.PROPERTY_EXTRA);
property.postValue(propertyExtra);
}
public void deleteProperty()
{
if(AppPreferences.getAgentName(//How to get context here?) == propertyExtra.agent)
{
}
}
}
视图类中的viewmodel定义:
viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this, getIntent().getExtras())).get(PropertyDetailActivityViewModel.class);
暂无答案!
目前还没有任何答案,快来回答吧!