'How should i bind viewList in android studio using Fragment and ViewModel class?
I want to create binding for listView named (id) list i was able to bind textView, but i have no idea how to bind list. Below i provide file i am working on. Is there any tutorial/docs how to works with ViewModel/fragment design patterns.
BillingDataFragment.java
public class BillingDataFragment extends Fragment {
private FragmentBillingDataBinding binding;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
BillingDataViewModel billingDataViewModel =
new ViewModelProvider(this).get(BillingDataViewModel.class);
binding = FragmentBillingDataBinding.inflate(inflater, container, false);
View root = binding.getRoot();
final TextView textView = binding.textGallery;
billingDataViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
final ListView listView = binding.list;
billingDataViewModel.getList().observe(getViewLifecycleOwner(), listView);
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
BillingDataViewModel:
public class BillingDataViewModel extends ViewModel {
private final MutableLiveData<String> mText;
private final MutableLiveData<ArrayList<String>> listItems;
ArrayAdapter<String> adapter;
public BillingDataViewModel() {
listItems = new MutableLiveData<>();
mText = new MutableLiveData<>();
mText.setValue("This is billing data fragment");
}
public LiveData<String> getText() {
return mText;
}
public LiveData<ArrayList<String>> getList() {
return listItems;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
