如何搜索包含特定动态值的列表
我有Model_BarcodeDetail
类型,当我在EditText上输入任何条形码其conatins像barcode, area,location,color
等 属性的列表,我想找寻条形码列表(列表可以有类似条形码具有相似的地区和位置或不同的面积和位置),如果我输入的条形码和我列表中的相似条形码具有相同的面积和位置,则我想doSomething()
其他doSomethingElse()
。如何搜索包含特定动态值的列表
我试过的代码是:
private List<String> barcodeList = new ArrayList<String>();
barcode = editText_barcode.getText().toString().trim();
if ((scanned_barcode != null
&& scanned_barcode.equalsIgnoreCase(barcode))) {
if ((!barcodeList.contains(barcode))) {
// if barcode I entered does not contains in the list
// It is working fine
barcodeList.add(barcode);//barcodeList contains only barcode
}
else if (barcodeList.contains(barcode)) {
data = list.get(barcodeList.indexOf(barcode));
// here is the problem
// here I want to get data of the barcode that have similar area and
location
if (data.getArea() == selected_area
&& data.getLocation() == selected_loc) {
doSomething();
} else {
doSomethingElse();
}
}
当你的名单看起来像:
List<Model_BarcodeDetail> list = new ArrayList<Model_BarcodeDetail>()
您可以使用foreach循环:
barcode = editText_barcode.getText().toString().trim();
if ((scanned_barcode != null
&& scanned_barcode.equalsIgnoreCase(barcode))) {
if ((!barcodeList.contains(barcode))) {
// if barcode I entered does not contains in the list
// It is working fine
}
for (Model_BarcodeDetail model_barcodeDetail : list) {
if (model_barcodeDetail.getArea() == selected_area && model_barcodeDetail.getLocation() == selected_loc) {
doSomething();
break;
}
}
// Nothing found
doSomethingElse();
}
搜索您的字符串在数组列表中并获取Object,然后检查条码的位置,这里是示例代码:
barcode = editText_barcode.getText().toString().trim();
if ((scanned_barcode != null
&& scanned_barcode.equalsIgnoreCase(barcode))) {
Model_BarcodeDetail model_barcodeDetail=getBarcodeDetails(barcode);
// for handling array do this in loop
if (model_barcodeDetail!=null && model_barcodeDetail.getArea() == selected_area && model_barcodeDetail.getLocation() == selected_loc) {
doSomething();
}else{
doSomethingElse();
}
}
/* your list can contain n number of similar bar code then change return type of this function to Model_BarcodeDetail[] */
private Model_BarcodeDetail getBarcodeDetails(Sttring barcode){
for (Model_BarcodeDetail model_barcodeDetail : list) {
if (barcode.eqauals(model_barcodeDetail.getBarcode)){
return model_barcodeDetail;
}
}
return null;
}
感谢它帮助我.. :) –
@SheenaTyagi马克答案接受,如果它是有帮助的 –
什么是'barcodeList'的数据类型 –
我真的不知道你想要什么。你想搜索并显示到你的列表视图或只是用'for'循环搜索? –