Question

I am currently creating an application that will read an NFC tag and look up the text of the tag against a string array to see if it is there. It works if the tag is case sensitive correct e.g. 'Test', not 'test'. I have tried various methods which haven't worked. Would someone please look at my code and see which is the best solution for me.

Here is the relevant code:

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();
            }
        }
    }
Était-ce utile?

La solution

Simple array search can do it:

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

And you can call it like this:

doesContain(dd, result);

Autres conseils

There is no native method to achieve this. you can try something like this.

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

The solution could be found if you don't do something case-insensitive, try to upper the first letter of any word before looking for it in the array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top