Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get string value from string array in Android
    primarykey
    data
    text
    <p>I am trying to create an application that reads an NFC tag and checks the tag against strings in a string array and then sets the text on another activity. I have got it working so that it checks if the string exists and sets the text in the new activity, but I want to be able to specify which string I want it to check against within the array, because there will be multiple strings in the NFC tag that I want to then display in the new activity. I have tried this for it:</p> <pre><code>result == getResources().getString(R.string.test_dd) </code></pre> <p>Here is the relevant code:</p> <pre><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(doesArrayContain(dd, result)) { Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(800); 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(); } } } </code></pre> <p><strong>EDIT:</strong></p> <p>Here is the method used and please can anyone help me with this problem:</p> <pre><code>public static boolean doesArrayContain(String[] array, String text) { for (String element : array) { if(element != null &amp;&amp; element.equalsIgnoreCase(text)) { return true; } } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload