【问题标题】:Android: comparing a comma separated string with an arraylist of stringAndroid:将逗号分隔的字符串与字符串的arraylist进行比较
【发布时间】:2012-04-26 13:19:21
【问题描述】:

我以逗号分隔字符串的形式从数据库中获取位置名称。我将此字符串与包含我输入的位置名称的字符串的 ArrayList 进行比较,以检查输入的位置名称是否与数据库中的位置名称相同。为此,我使用以下代码。但这似乎不起作用。请让我怎么做。我也使用了 equals(),但它不能正常工作。

String loc =DataHelperdh.getLocationList(id); // loc holds the comma separated locations

for (int i2 = 0; i2 < locationList.size(); i2++)
{                               
  if(locationList.get(i2)==null || locationList.get(i2).equals(""))continue;
      String str1 =loc.toString();
      int result = str1.indexOf(locationList.get(i2));
      if(result > -1){  
          Toast.makeText(getApplicationContext(), "Locations with same name cannot exist same", Toast.LENGTH_LONG).show();
          break;
   }
}

【问题讨论】:

    标签: android equals indexof


    【解决方案1】:

    您检查str1 是否包含整个列表,这不太可能。如果你把它转过来,它应该可以工作。

    String loc = DataHelperdh.getLocationList(id); // loc holds the comma separated locations
    
    for (int i2 = 0; i2 < locationList.size(); i2++) {
      String test = locationList.get(i2);                        
      if(test == null || test.isEmpty()) continue;
      int result = loc.indexOf(test);
      if(result > -1) {  
        Toast.makeText(getApplicationContext(), "Locations with same name cannot exist same", Toast.LENGTH_LONG).show();
        break;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多