Android 安卓地图<&燃气轮机;如果对象的值为真,如何使用键名设置text()

Android 安卓地图<&燃气轮机;如果对象的值为真,如何使用键名设置text(),android,arraylist,hashmap,google-cloud-firestore,Android,Arraylist,Hashmap,Google Cloud Firestore,我从firestore获取数据,并将其放入映射中。然后,我想用setText方法显示具有真实值的数据。但是我使用List存储值来检查是否存在重复循环时出现了问题。因此我意识到,具有真值的键是重复循环的,因为有映射检查的size()值为21。因此,它循环21次,并重复将值放入列表中。我怎样才能循环一次以获得所有具有真值的键 我的代码如下: private Map<String, Object> check = new HashMap<>(); //declare vari

我从
firestore
获取数据,并将其放入
映射中
。然后,我想用
setText
方法显示具有真实值的数据。但是我使用
List
存储值来检查是否存在重复循环时出现了问题。因此我意识到,具有真值的键是重复循环的,因为有
映射检查
的size()值为21。因此,它循环21次,并重复将值放入
列表中。我怎样才能循环一次以获得所有具有真值的

我的代码如下:

private Map<String, Object> check = new HashMap<>();  //declare variable
private List<String> amenityName= new ArrayList<>(); //declare variable

if(check != null){   //check is not null
     for(Map.Entry<String, Object> entry : check.entrySet() ){
                   String key= entry.getKey();
                   Object value = entry.getValue();
                   if(value != null){
                        amenityName.add(key);
                   }
                }
             amenityName.size();   // check the size
}
private Map check=new HashMap()//声明变量
私有列表修正名=新的ArrayList()//声明变量
如果(check!=null){//check不为null
对于(Map.Entry:check.entrySet()){
String key=entry.getKey();
对象值=entry.getValue();
if(值!=null){
amendityname.add(键);
}
}
amendityName.size();//检查大小
}
地图检查中的数据存储如下图所示:

修改名称.size()
如下图所示:


如果我没弄错你的问题。您希望获得等于“true”的字符串值

您只需将
for
块包装在
if
语句中即可完成此操作

大概是这样的:

private Map<String, String> check = new HashMap<>();  //declare variable
private List<String> amenityName= new ArrayList<>(); //declare variable

if(check != null){   //check is not null
     for(Map.Entry<String, String> entry : check.entrySet() ){
                   String key= entry.getKey();
                   String value = entry.getValue();
                   if(value != null && value.equalIgnoreCase("true")){
                        amenityName.add(key);
                   }
                }
             amenityName.size();   // check the size
}

如果我没弄错你的问题。您希望获得等于“true”的字符串值

您只需将
for
块包装在
if
语句中即可完成此操作

大概是这样的:

private Map<String, String> check = new HashMap<>();  //declare variable
private List<String> amenityName= new ArrayList<>(); //declare variable

if(check != null){   //check is not null
     for(Map.Entry<String, String> entry : check.entrySet() ){
                   String key= entry.getKey();
                   String value = entry.getValue();
                   if(value != null && value.equalIgnoreCase("true")){
                        amenityName.add(key);
                   }
                }
             amenityName.size();   // check the size
}

是的,您可以在不使用for循环的情况下找到值为true的所有键。请参阅下面的示例:-

   //below is the hash map
    Map<String, Object> check = new HashMap<>();
    check.put("abc",true);
    check.put("zzz","");
    check.put("aaa",true);
    check.put("eee",null);
    check.put("rrr",true);

   //Retain all the pairs in map whose value is true
   check.values().retainAll(Collections.singleton(true));

    //add the filtered map to array list
    List<String> stringList=new ArrayList<>();
    stringList.addAll(check.keySet());

    Log.e("TAG", "onCreate************: "+check.size()+"****"+stringList );
//下面是哈希映射
映射检查=新建HashMap();
检查。填写(“abc”,正确);
勾选。放入(“zzz”,“zzz”);
检查。输入(“aaa”,正确);
检查。输入(“eee”,空);
检查。输入(“rrr”,正确);
//保留映射中值为true的所有对
check.values().retainAll(Collections.singleton(true));
//将过滤后的映射添加到数组列表
List stringList=新建ArrayList();
stringList.addAll(check.keySet());
Log.e(“TAG”,“onCreate*********:”+check.size()+“****”+stringList);

是的,您可以在不使用for循环的情况下找到所有值为true的键。请参阅下面的示例:-

   //below is the hash map
    Map<String, Object> check = new HashMap<>();
    check.put("abc",true);
    check.put("zzz","");
    check.put("aaa",true);
    check.put("eee",null);
    check.put("rrr",true);

   //Retain all the pairs in map whose value is true
   check.values().retainAll(Collections.singleton(true));

    //add the filtered map to array list
    List<String> stringList=new ArrayList<>();
    stringList.addAll(check.keySet());

    Log.e("TAG", "onCreate************: "+check.size()+"****"+stringList );
//下面是哈希映射
映射检查=新建HashMap();
检查。填写(“abc”,正确);
勾选。放入(“zzz”,“zzz”);
检查。输入(“aaa”,正确);
检查。输入(“eee”,空);
检查。输入(“rrr”,正确);
//保留映射中值为true的所有对
check.values().retainAll(Collections.singleton(true));
//将过滤后的映射添加到数组列表
List stringList=新建ArrayList();
stringList.addAll(check.keySet());
Log.e(“TAG”,“onCreate*********:”+check.size()+“****”+stringList);

不,我可以理解的问题是,他在列表中遇到了重复值的问题。使用true string添加值不是问题。感谢您的尝试。但他对真字符串所做的检查是错误的。这就是为什么我用这样的方式解释我的答案,你做得很好。我只是专注于他遇到的真正问题。感谢你的努力。哈哈,我只是忘了用大写的“真”来检查,谢谢mansNo,这个问题我可以理解,他在列表中遇到了重复值的问题。使用true string添加值不是问题。感谢您的尝试。但他对真字符串所做的检查是错误的。这就是为什么我用这样的方式解释我的答案你做得很好。我只是专注于他所遇到的真正问题。感谢你的努力。哈哈,我只是忘记了用大写的“真”来检查,谢谢你。请添加你的数据库结构,以便更清楚地查看数据。请添加你的数据库结构,以便更清楚地查看数据。