android - 对话框片段中的微调器始终为空 - 包含带有值但不显示它们的适配器
问题描述
我是 Android 新手,在 DialogFragment 中使用 Spinners 时遇到了一些问题。
我正在尝试创建一个带有两个微调器的对话框片段,它们都填充了来自字符串数组资源的数据。
我没有收到任何错误,并且在使用调试器单步执行时,我可以看到在调用适当的方法后,微调器包含包含来自数组的数据的适配器。
但是,当对话框打开时,微调器始终显示为空。
我花了几个小时扫描我的代码以查找错误,翻阅文档,并查看相关帖子。
其他空微调器的情况,例如在此处找到的Spinner 不会在片段中显示下拉菜单,而此处Spinner 显示为空但包含数据似乎是由微调器的设置代码问题引起的,但我在这里找不到类似的东西。
我认为这可能与视图绑定有关,因此我尝试使用 findViewById() 来获取视图的引用,但这不起作用。
我想知道这是否可能是 Fragment 或 DialogFragment 特有的问题,因为我在 Activity 中没有遇到这种情况。
我被困住了,我将不胜感激任何人都可以提供的指导!
对话片段:
'''
public class AddGoalDialog extends DialogFragment {
FragmentAddGoalDialogBinding binding;
public AddGoalDialog() {
// Required empty public constructor
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){
return new AlertDialog.Builder(requireContext())
.setTitle(getString(R.string.new_goal_dialog_title))
.setPositiveButton(getString(R.string.submit), (dialog, id) -> {
doPositiveClick();
} )
.setNegativeButton(getString(R.string.cancel), (dialog, id) -> {
dialog.cancel();
})
.setView(R.layout.fragment_add_goal_dialog)
.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
binding = FragmentAddGoalDialogBinding.bind(view);
ArrayAdapter<CharSequence> durAdapter = ArrayAdapter.createFromResource(getContext(),
R.array.goal_duration_array, android.R.layout.simple_spinner_item);
durAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dialogSpinnerGoalDuration = view.findViewById(R.id.dialog_spinner_goal_duration);
dialogSpinnerGoalDuration.setAdapter(durAdapter);
ArrayAdapter<CharSequence> typeAdapter = ArrayAdapter.createFromResource(getContext(),
R.array.goal_type_array, android.R.layout.simple_spinner_item);
typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dialogSpinnerGoalType = binding.dialogSpinnerGoalType;
dialogSpinnerGoalType.setAdapter(typeAdapter);
super.onViewCreated(view, savedInstanceState);
}
}
'''
对话框片段布局:
'''
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".AddGoalDialog">
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/spinner_goal_duration"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner_goal_type"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText_enter_goal"
android:inputType="number"
android:hint="@string/enter_a_goal"/>
</LinearLayout>
'''
解决方案
你试过这样吗?
ArrayAdapter<String> adapter = new ArrayAdapter<String> ( getContext (), android.R.layout.simple_spinner_dropdown_item, yourArrayList );
推荐阅读
- json - 如何使用 jq 解析在 json 中编码为字符串的 json
- git - 尽管文件不存在合并冲突
- java - 在 libgdx 中构建失败
- amazon-web-services - 无服务器 | 如何将 Lambda 函数作为触发器连接到 DynamoDB 表
- javascript - Vue - 将过渡应用于菜单元素
- c - 未定义对“BIO_s_mem”的引用
- graphics - Direct3D中绘制顶点范围的方法
- vuejs2 - 如何在包含数据的表中显示 json 文件中的特定字段?
- javascript - 带有缩略图的 javascript 滑块不能同时使用这两个选项
- java - Localstack - 通过 java aws sdk 访问 SNS:NoSuchFieldError: ENDPOINT_OVERRIDDEN