简体   繁体   English

使用 Android 中另一个片段的 EditText 填充微调器

[英]Populating spinner with EditText from another fragment in Android

I have two fragment.In first one is EditText witch taking some info from user,and in another fragment having Spinner.I am sending EditText,that was first converted to String, to Spinner in second fragment with Android Navigation.And it shows text just fine but when i wish to add another text to Spinner it just replace first text and showing only one text/item in spinner.我有两个片段。在第一个是 EditText 女巫从用户那里获取一些信息,在另一个片段中具有 Spinner。我正在使用 Android Navigation 将首先转换为字符串的 EditText 发送到第二个片段中的 Spinner。它只显示文本很好,但是当我想向 Spinner 添加另一个文本时,它只是替换第一个文本并在微调器中只显示一个文本/项目。 So my question how to have save text that i previously sent and have nice drop down spinner.所以我的问题是如何保存我之前发送的文本并拥有不错的下拉微调器。 BTW i am using data binding i don't know if it is important information for solutions.顺便说一句,我正在使用数据绑定,我不知道它是否是解决方案的重要信息。

fragment from where i am sending data:我发送数据的片段:

@Override
public void onClickComplete() {
    String addCategoryString = addCategory.getText().toString();
    FluctuatingCategoryFragmentDirections.ActionFluctuatingCategoryToCreatingBudgetingItem2 action
            = FluctuatingCategoryFragmentDirections.actionFluctuatingCategoryToCreatingBudgetingItem2();
    action.setFluctuatingCategory(addCategoryString);
    navController.navigate(action);
}

receiving fragment:接收片段:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (getArguments() != null) {
        CreatingBudgetingItemFragmentArgs args = CreatingBudgetingItemFragmentArgs.fromBundle(getArguments());
        String getCategory = args.getFluctuatingCategory();
        Log.i("Category: ", getCategory);
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, android.R.id.text1);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
        spinnerAdapter.notifyDataSetChanged();
    }
}

When you are navigating to FluctuatingCategoryToCreatingBudgetingItem , the fragment is getting recreated every time, that's why only the value you passed will show up.当您导航到FluctuatingCategoryToCreatingBudgetingItem时,每次都会重新创建片段,这就是为什么只会显示您传递的值的原因。 You must use ViewModel.您必须使用 ViewModel。

Setup a list which will contain the FluctuatingCategoryies and setup your CreatingBudgetingItemFragment to get list item from ViewModel.设置一个包含 FluctuatingCategoryies 的列表并设置您的CreatingBudgetingItemFragment以从 ViewModel 获取列表项。

Not giving you any code because I want you to start your learning curve.不给你任何代码,因为我希望你开始你的学习曲线。 This might help. 可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM