Android:将逗号分隔的字符串与字符串的arraylist进行比较
我以逗号分隔字符串的形式从数据库中获取位置名称。我将此字符串与包含我输入的位置名称的字符串的ArrayList进行比较,以检查输入的位置名称是否与数据库中的位置名称相同。为此,我使用下面的代码。但它似乎不起作用。请告诉我怎么办。我也用了equals,但效果不好Android:将逗号分隔的字符串与字符串的arraylist进行比较,android,equals,indexof,Android,Equals,Indexof,我以逗号分隔字符串的形式从数据库中获取位置名称。我将此字符串与包含我输入的位置名称的字符串的ArrayList进行比较,以检查输入的位置名称是否与数据库中的位置名称相同。为此,我使用下面的代码。但它似乎不起作用。请告诉我怎么办。我也用了equals,但效果不好 String loc =DataHelperdh.getLocationList(id); // loc holds the comma separated locations for (int i2 = 0; i2 < loca
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;
}
}
您可以检查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;
}
}