我有一个Model_BarcodeDetail
类型的列表,其中包含barcode, area,location,color
等属性。
当我在edittext中输入任何条形码时,我想在列表中搜索该条形码(列表可以有n个类似的条形码,具有相似的区域和位置或不同的区域和位置),如果条形码我输入并且我列表中的类似条形码具有相同的区域和位置,然后我想要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();
}
}
答案 0 :(得分:1)
在数组列表中搜索字符串并获取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;
}
答案 1 :(得分:0)
当您的列表看起来像:
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();
}