android-viewmodel

ViewModel cannot be instantiated in an Activity

巧了我就是萌 提交于 2020-12-30 06:19:54
问题 I am trying to instantiate one ViewModel to use across all of my Activity(s). public class LaunchActivity extends Activity { private Controller control; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch); control = ViewModelProviders.of(this).get(Controller.class); } } but I got an error at control = ViewModelProviders.of(this) <-- stating that it cannot resolve of(this) , but based on the example here by

Android Two Way DataBinding Problem of Ternary Operator Must be Constant

痞子三分冷 提交于 2020-12-23 08:22:32
问题 My EditText is like this: <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="@={viewModel.isAddCase? ``: `` + viewModel.currentStudent.age}" //problem here android:inputType="number" /> I want the EditText not to show anything (empty String) based on the isAddCase variable, which is a MutableLiveData<Boolean> initilized when the ViewModel class object is created (inside the init{} block). This is the error I got: The expression '(

Android Two Way DataBinding Problem of Ternary Operator Must be Constant

这一生的挚爱 提交于 2020-12-23 08:20:52
问题 My EditText is like this: <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="@={viewModel.isAddCase? ``: `` + viewModel.currentStudent.age}" //problem here android:inputType="number" /> I want the EditText not to show anything (empty String) based on the isAddCase variable, which is a MutableLiveData<Boolean> initilized when the ViewModel class object is created (inside the init{} block). This is the error I got: The expression '(

Updating value inside fragment from activity

☆樱花仙子☆ 提交于 2020-12-15 16:38:42
问题 I wish to transfere my old app to MVVM, so can you please help me with this test: Shared ViewModel: class SharedViewModel : ViewModel() { private var _numberF1 = MutableLiveData<Int>().apply { postValue(0)} val numberF1: LiveData<Int> get() = _numberF1 fun addNumberF1() { _numberF1.value = _numberF1.value?.plus(1) } } Fragment: class Fragment1 : Fragment() { companion object { fun newInstance() = Fragment1() } private lateinit var viewModel: SharedViewModel override fun onCreateView(inflater:

Updating value inside fragment from activity

心不动则不痛 提交于 2020-12-15 16:36:40
问题 I wish to transfere my old app to MVVM, so can you please help me with this test: Shared ViewModel: class SharedViewModel : ViewModel() { private var _numberF1 = MutableLiveData<Int>().apply { postValue(0)} val numberF1: LiveData<Int> get() = _numberF1 fun addNumberF1() { _numberF1.value = _numberF1.value?.plus(1) } } Fragment: class Fragment1 : Fragment() { companion object { fun newInstance() = Fragment1() } private lateinit var viewModel: SharedViewModel override fun onCreateView(inflater:

Updating value inside fragment from activity

独自空忆成欢 提交于 2020-12-15 16:35:06
问题 I wish to transfere my old app to MVVM, so can you please help me with this test: Shared ViewModel: class SharedViewModel : ViewModel() { private var _numberF1 = MutableLiveData<Int>().apply { postValue(0)} val numberF1: LiveData<Int> get() = _numberF1 fun addNumberF1() { _numberF1.value = _numberF1.value?.plus(1) } } Fragment: class Fragment1 : Fragment() { companion object { fun newInstance() = Fragment1() } private lateinit var viewModel: SharedViewModel override fun onCreateView(inflater:

Cannot create an instance of class ViewModel in the new project

强颜欢笑 提交于 2020-12-13 03:35:31
问题 I have two different projects. In the first one I use ViewModel with my activity and Room database. Everything works. Then I just created new project for different purposes and there I instantiate ViewModel absolutely the same way, but my app crashes with this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.MainViewModel at android.app.ActivityThread

Cannot create an instance of class ViewModel in the new project

余生颓废 提交于 2020-12-13 03:31:01
问题 I have two different projects. In the first one I use ViewModel with my activity and Room database. Everything works. Then I just created new project for different purposes and there I instantiate ViewModel absolutely the same way, but my app crashes with this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.MainViewModel at android.app.ActivityThread

Share Viewmodel between BottomSheetDialogFragment with normal Fragment not working (observe function not trigger)

∥☆過路亽.° 提交于 2020-12-05 12:15:19
问题 I want to share the same Viewmodel between my base fragment along with opened BottomSheetDialogFragment So this is how i observe to same viewmodel between these two fragment. BottomSheetDialogFragment public class TasteFilterBottomDialogFragment extends BottomSheetDialogFragment { private FilterTasteListViewModel filterTasteListViewModel; @Inject ViewModelProvider.Factory viewModelFactory; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated

Unit test the new Kotlin coroutine StateFlow

懵懂的女人 提交于 2020-12-05 06:55:49
问题 Recently was introduced the class StateFlow as part of Kotlin coroutine. I'm currently trying it and encounter an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel. My code is as follow: ViewModel: class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() { val userWallet: StateFlow<State<UserWallets>> get() = _userWallets private val _userWallets: