0

我目前正在创建一个应用程序,它将读取 NFC 标签并根据字符串数组查找标签的文本以查看它是否存在。如果标签区分大小写正确,例如“测试”而不是“测试”,它就可以工作。我尝试了各种方法都没有奏效。有人可以看看我的代码,看看哪个对我来说是最好的解决方案。

以下是相关代码:

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

3 回答 3

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);
于 2013-07-23T08:29:49.877 回答
0

没有本地方法可以实现这一点。你可以尝试这样的事情。

public static boolean ContainsCaseInsensitive(ArrayList<String> searchList, String searchTerm)
{
    for (String item : searchList)
    {
        if (item.equalsIgnoreCase(searchTerm)) 
            return true;
    }
    return false;
}
于 2013-07-23T08:30:04.103 回答
-1

如果您不做不区分大小写的操作,则可以找到解决方案,在数组中查找之前尝试将任何单词的第一个字母大写。

于 2013-07-23T08:29:43.720 回答