2013-07-23 50 views
0

我目前正在創建一個應用程序,該應用程序將讀取NFC標籤,並根據字符串數組查看標籤的文本以查看它是否存在。如果標籤區分大小寫正確無誤'測試',而不是'測試'。我嘗試了各種方法,沒有奏效。請有人看看我的代碼,看看哪個是最適合我的解決方案。如何在Android中使字符串數組不區分大小寫

下面是相關的代碼:如果你不這樣做的東西不區分大小寫,嘗試上的任何單詞的第一個字母數組中尋找它之前

String[] dd; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    dd = getResources().getStringArray(R.array.device_description); 

} 

@Override 
    protected void onPostExecute(String result) { 
     if (result != null) { 
      if(Arrays.asList(dd).contains(result)) { 
      Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 
      v.vibrate(800); 
      //mText.setText("Read content: " + result); 
      Intent newIntent = new Intent(getApplicationContext(), TabsTest.class); 
      Bundle bundle1 = new Bundle(); 
      bundle1.putString("key", result); 
      newIntent.putExtras(bundle1); 
      startActivity(newIntent); 
      Toast.makeText(getApplicationContext(), "NFC tag written successfully!", Toast.LENGTH_SHORT).show(); 
      } 
      else{ 
       Toast.makeText(getApplicationContext(), result + " is not in the device description!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
+0

您可以編寫一個自定義方法來檢查該方法,而不是依賴於API方法。 – NINCOMPOOP

回答

2

簡單陣列搜索可以做到這一點:

public static boolean doesArrayContain(String[] array, String text) { 
    for (String element : array) 
     if(element != null && element.equalsIgnoreCase(text)) { 
      return true; 
     } 
    } 
    return false; 
} 

你可以這樣調用:

doesContain(dd, result); 
+0

非常感謝!這個解決方案爲我工作! –

-1

將可以找到解決辦法。

+0

-1對於一個bassackwards解決方案 – paulkayuk

0

有實現這個原始方法。你可以嘗試這樣的事情。

public static boolean ContainsCaseInsensitive(ArrayList<String> searchList, String searchTerm) 
{ 
    for (String item : searchList) 
    { 
     if (item.equalsIgnoreCase(searchTerm)) 
      return true; 
    } 
    return false; 
} 
+0

謝謝您的建議。我怎樣纔能有效地實現我的代碼? –

+0

只需調用此方法(ContainsCaseInsensitive(Arrays.asList(dd),result)){Your Code} –

相關問題