0

我以逗号分隔字符串的形式从数据库中获取位置名称。我将此字符串与包含我输入的位置名称的字符串的 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;
   }
}
4

1 回答 1

0

您检查是否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;
  }
}
于 2012-04-26T13:29:13.747 回答