2015-11-03 52 views
0

我有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(); 
      } 

     } 
+0

什麼是'barcodeList'的數據類型 –

+0

我真的不知道你想要什麼。你想搜索並顯示到你的列表視圖或只是用'for'循環搜索? –

回答

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(); 

    } 
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; 
    } 
+0

感謝它幫助我.. :) –

+0

@SheenaTyagi馬克答案接受,如果它是有幫助的 –

相關問題