Java 列表值和我的给定值相等,那么复选框将被选中?
我正在用安卓系统做这个项目。我的情况我将列表值存储在firebase实时数据库中,然后列表值和我的给定值相等,复选框将被选中 例如:列表包含36,40,44。那么我的给定值等于 设置为该列表值后,将选中等效复选框。 我的代码是Java 列表值和我的给定值相等,那么复选框将被选中?,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在用安卓系统做这个项目。我的情况我将列表值存储在firebase实时数据库中,然后列表值和我的给定值相等,复选框将被选中 例如:列表包含36,40,44。那么我的给定值等于 设置为该列表值后,将选中等效复选框。 我的代码是 checkcheck=FirebaseDatabase.getInstance().getReference().child("Products").child(newprokey); checkcheck.child("size").
checkcheck=FirebaseDatabase.getInstance().getReference().child("Products").child(newprokey);
checkcheck.child("size").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> areas = new ArrayList<String>();
for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
String areaName = areaSnapshot.child("Value").getValue(String.class);
areas.add(areaName);
}
String[] check = areas.toArray(new String[areas.size()]);
for (int i= 0;i<check.length;i++)
{
if (check[i] == "36")
{
jS36.setChecked(true);
}
if (check[i] == "38")
{
jM38.setChecked(true);
}
if (check[i] == "40")
{
jL40.setChecked(true);
}
if (check[i] == "42")
{
jXl42.setChecked(true);
}
if (check[i] == "44")
{
jXxl44.setChecked(true);
}
if (check[i] == "46")
{
jXXXl46.setChecked(true);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我试试这个代码。它不起作用,请给出解决方案。您应该使用.equals来比较字符串,而不是==检查此项
此外,还可以遍历列表,而不是创建数组
最终列表区域=新的ArrayList;
对于DataSnapshot areaSnapshot:DataSnapshot.getChildren{
字符串areaName=areaSnapshot.childValue.getValueString.class;
areas.addareaName;
}
对于字符串区域:区域{
ifarea.equals 36{
jS36.setCheckedtrue;
}
ifarea.equals 38{
jM38.setCheckedtrue;
}
//继续
}
编辑
更简单的解决方案是创建复选框的映射,并直接使用areaName更改状态
//在调用'findViewById'引用复选框后,在该映射中初始化复选框
HashMap checkBoxHashMap=新的HashMap;
checkBoxHashMap.put36,js36;
checkBoxHashMap.put38,jM38;
//添加带有区域名称的所有复选框
对于DataSnapshot areaSnapshot:DataSnapshot.getChildren{
字符串areaName=areaSnapshot.childValue.getValueString.class;
//确保areaName在复选框HashMap中为有效值
ifcheckBoxHashMap.containsKeyareaName{
checkBoxHashMap.getareaName.setCheckedtrue;
}
}
非常感谢。它工作得很好。。但是我不能理解这个“字符串区域:区域”的代码,你能解释一下吗..检查这个