I am new to android app development. Please help. I want to get navigation fragment arguments in my view model class directly. I am using data-binding.
Here is my nav fragments argument
<fragment>
......
..
<action
android:id="@ id/action_update_to_listOfStudentFragment"
app:destination="@id/list_fragment" />
<argument
android:name="currentStudent"
app:argType="com.example.school.model.Student" />
</fragment>
When I click on my recycler view item I pass the argument object with navigation action. So I set on click listener in adapters onbind function. I am passing student objects as arguments.
holder.itemLayoutBinding.root.setOnClickListener {
val action = ListOfStudentFragmentDirections.actionListOfStudentFragmentToUpdate(currentStudent)
holder.itemLayoutBinding.root.findNavController().navigate(action)
}
My View Model is here. I want to know another thing: I am using AndroidViewModel here for database context. Is there any other way to avoid AndroidViewModel? I want to use ViewModel but the view model does not provide context.
class MainModleView(application: Application) : AndroidViewModel(application) {
val getAllStudentData: LiveData<List<Student>>
val repository: Repository
val inputName = MutableLiveData<String>()
val inputAge = MutableLiveData<String>()
init {
val studentDao: StudentDao = StudentDatabase.getAllData(application).studentDao()
repository = Repository(studentDao)
getAllStudentData = repository.getAllData
}
So in View-Model's live data, I want to set the inputName and inputAge value from fragments arguments. Here is a sample of what I want.
inputName.value = fragmentsarugments.name //arguments pass a model object from recyclerview on click function
inputAge.value = fragmentsarugments.age
Please give me a solution. I only want to get fragment's argument in my ViewModel.
CodePudding user response:
Use a ViewModelProviderFactory
for custom arguments. You can pass your arguments directly when initializing your viewmodel.
class MeterListViewModelFactory implements ViewModelProvider.Factory {
private final MainRepository mainRepository;
private final Bundle mExtras;
private final Context context;
public MeterListViewModelFactory(Context context, MainRepository mainRepository, Bundle mExtras) {
this.mainRepository = mainRepository;
this.context = context;
this.mExtras = mExtras;
}
@NonNull
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.equals(MeterListViewModel.class))
//noinspection unchecked
return (T) new MeterListViewModel(context,mainRepository, mExtras);
return super.create(modelClass);
}
}
Create viewmodel like this
viewModel = new ViewModelProvider(this, new MeterListViewModelFactory(LdpApplication.getAppContext(), new MainRepository(new MainRemoteDataSource()
, new MainCachedDataSource(AppRoomDatabaseImpl.getInstance(requireContext()).getAppDatabase())), getArguments())).get(MeterListViewModel.class);
Then you can initialize your variables from args passed
public MeterListViewModel(Context context, MainRepository mainRepository, Bundle mExtras) {
this.mainRepository = mainRepository;
mContext = context;
if (mExtras != null && mExtras.containsKey(AppConstants.K_METER_TYPE))
metersType.setValue((MetersType) mExtras.getSerializable(AppConstants.K_METER_TYPE));
if (mExtras.getString(AppConstants.K_COMMISSION_FILTER).equalsIgnoreCase(context.getString(R.string.txt_all))) {
lastSelectedPosition = 0;
} else if (mExtras.getString(AppConstants.K_COMMISSION_FILTER).equalsIgnoreCase(context.getString(R.string.txt_sign_off_pending))) {
lastSelectedPosition = 1;
} else if (mExtras.getString(AppConstants.K_COMMISSION_FILTER).equalsIgnoreCase(context.getString(R.string.txt_sign_off_pending_for_approval))) {
lastSelectedPosition = 2;
} else if (mExtras.getString(AppConstants.K_COMMISSION_FILTER).equalsIgnoreCase(context.getString(R.string.txt_sign_off_done))) {
lastSelectedPosition = 3;
} else {
lastSelectedPosition = 4;
}
selectedFilterItem = mExtras.getString(AppConstants.K_COMMISSION_FILTER);
commissionFilters = HelperClass.getCommissionFilters(context);
this.loadMeters(null, true);
}
CodePudding user response:
pass arguments to viewmodel
StudentFragment :Fragment(){
private val args: StudentFragmentArgs by navArgs()
private var viewModel:ViewModel?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get(MainModleView.class)
vviewModel.inputName.value = args.Student
}
}