DialogFragment 中的 ViewModel
ViewModel in DialogFragment
我正在尝试更改此 class 以处理我的房间查询
@Query("select * from customer_contacts where serverCustomerId = :id and contactName like '%' || :name || '%'")
fun fetchCustomerContactsByName(id:Int, name:String): LiveData<List<CustomerContact>>
和我的视图模型
class CustomerContactVM : ViewModel() {
var customers: LiveData<List<CustomerContact>> = MutableLiveData<List<CustomerContact>>()
fun getCustomerContacts(id: Int, name: String) {
customers = CustomerContactDao().fetchCustomerContactsByName(id, name)
}
}
我不明白如何在对话框片段中创建视图模型,因为我不断遇到无法解决尝试 ViewModelProviders.of(this).get(CustomerContactVM.class)
的错误
public class CustomerContactListFragment extends DialogFragment {
private CustomerContactVM customerContactVM;
@Override
public void onStart() {
super.onStart();
//setEmptyText("No Items");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (savedInstanceState != null) { mCustomer = savedInstanceState.getString(ARG_CUSTOMER);
if (savedInstanceState.getString(ARG_QUERY) != null) {
mQuery = savedInstanceState.getString(ARG_QUERY);
}
}
GlobalState globalState = (GlobalState) getActivity().getApplication();
// mState.addScreenLog("CustomerContactListFragment");
return inflater.inflate(R.layout.fragment_catalogue_items, container, false);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
dbHelper.close();
super.onDetach();
}
}
ViewModelProviders.of (this) 已弃用。所以你应该使用最新版本的生命周期组件。
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc03"
//for kotlin
implementation "group: 'androidx.lifecycle', name:'lifecycle-viewmodel-ktx', version:2.2.0-rc03"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-rc03"
ViewModel 初始化(Java):
private CustomerContactVM customerContactVM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customerContactVM = ViewModelProvider(this).get(CustomerContactVM.class);
// Other setup code below...
}
ViewModel 初始化 (Kotlin):
var customerContactVM:CustomerContactVM? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
customerContactVM = ViewModelProvider(this).get(CustomerContactVM::class.java)
// Other setup code below...
}
如果您将 DialogFragment 用作 activity 中的对话框并希望引用此 activity 的 ViewModel(而不是为 DialogFragment 创建一个新的),您应该使用:
private CustomViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProvider(requireActivity()).get(CustomViewModel .class);
// Other setup code below...
}
我正在尝试更改此 class 以处理我的房间查询
@Query("select * from customer_contacts where serverCustomerId = :id and contactName like '%' || :name || '%'")
fun fetchCustomerContactsByName(id:Int, name:String): LiveData<List<CustomerContact>>
和我的视图模型
class CustomerContactVM : ViewModel() {
var customers: LiveData<List<CustomerContact>> = MutableLiveData<List<CustomerContact>>()
fun getCustomerContacts(id: Int, name: String) {
customers = CustomerContactDao().fetchCustomerContactsByName(id, name)
}
}
我不明白如何在对话框片段中创建视图模型,因为我不断遇到无法解决尝试 ViewModelProviders.of(this).get(CustomerContactVM.class)
的错误public class CustomerContactListFragment extends DialogFragment {
private CustomerContactVM customerContactVM;
@Override
public void onStart() {
super.onStart();
//setEmptyText("No Items");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (savedInstanceState != null) { mCustomer = savedInstanceState.getString(ARG_CUSTOMER);
if (savedInstanceState.getString(ARG_QUERY) != null) {
mQuery = savedInstanceState.getString(ARG_QUERY);
}
}
GlobalState globalState = (GlobalState) getActivity().getApplication();
// mState.addScreenLog("CustomerContactListFragment");
return inflater.inflate(R.layout.fragment_catalogue_items, container, false);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
dbHelper.close();
super.onDetach();
}
}
ViewModelProviders.of (this) 已弃用。所以你应该使用最新版本的生命周期组件。
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc03"
//for kotlin
implementation "group: 'androidx.lifecycle', name:'lifecycle-viewmodel-ktx', version:2.2.0-rc03"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-rc03"
ViewModel 初始化(Java):
private CustomerContactVM customerContactVM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customerContactVM = ViewModelProvider(this).get(CustomerContactVM.class);
// Other setup code below...
}
ViewModel 初始化 (Kotlin):
var customerContactVM:CustomerContactVM? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
customerContactVM = ViewModelProvider(this).get(CustomerContactVM::class.java)
// Other setup code below...
}
如果您将 DialogFragment 用作 activity 中的对话框并希望引用此 activity 的 ViewModel(而不是为 DialogFragment 创建一个新的),您应该使用:
private CustomViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProvider(requireActivity()).get(CustomViewModel .class);
// Other setup code below...
}