【发布时间】:2023-03-19 19:58:01
【问题描述】:
基本上,我在三 (3) 个不同的片段中有三 (3) 个复选框列表。当我打开活动时,默认片段中的复选框正常显示,但不同片段中的复选框显示不同。复选框的值仍然为真。下面是复选框的图像,使事情更清楚,也是我的代码。
第一个片段页面:
第二个片段页面:
设置复选框的代码及其if condition
private void setCheckBox(ArrayList<DataPrefType> arrayList){
for(int i = 0; i < arrayList.size(); i++){
id = i + 1;
checkBox = new CheckBox(getActivity());
checkBox.setId(i + 1);
checkBox.setText(arrayList.get(i).getName());
checkBox.setPadding(10, 10, 10, 10);
checkBox.setLayoutParams(params);
checkBox.setGravity(Gravity.CENTER);
checkBoxLayout.addView(checkBox);
if(!userPreference.isEmpty() || userPreference != null){
for(int j = 0; j < userPreference.get(0).getDataPrefTypeArrayList().size(); j++){
int retrieveId = Integer.parseInt(userPreference.get(0).getDataPrefTypeArrayList().get(j).getId());
if(checkBox.getId() == retrieveId)
checkBox.setChecked(true);
}
}
【问题讨论】:
-
不是答案,而是提示...在您的第一个 if 语句中,您应该首先检查
userPreference是否为空(在!userPreference.isEmpty()之前)并将||替换为&&所以两个条件都必须为真 -
好的。谢谢你的提示:)
-
我面临着完全相同的问题。我在选项卡式活动中有 2 个不同的片段,每个片段都有复选框。我将数据加载到基于 OnViewCreated 的复选框中。在第二个片段中,真正的复选框被突出显示而不是勾选。由于选项卡式活动,我觉得这是一个问题。
-
我可以确认问题总是从第二个选项卡开始出现。我切换了标签位置,但问题仍然存在于第二个标签上。
标签: java android android-fragments checkbox