title: Get adapter items in Android
body: <p>I have an AutoCompleteTextView filled with an ArrayAdapter of strings. I add on my adapter some string dynamically:</p>
  
  <pre><code>adapter.add("foo");
  </code></pre>
  
  <p>But I want to check if the string that I want to add to the adapter is already inside it, and for doing this, I think it is necessary to get all the items contents in the adapter for checking them.</p>
  
  <p>I've tried to use:</p>
  
  <pre><code>adapter.getItem(position)
  </code></pre>
  
  <p>But it doesn't work and crashes my app. How can I check the strings in the adapter?</p>
  
  <h3>Code</h3>
  
  <pre><code>public class myTTSActivity extends Activity {
      private TextToSpeech tts;
      private AutoCompleteTextView txtText;
      private int prefN;
      private ArrayAdapter&lt;String&gt; adapter;
  
      private static final String PREF_NAME = "PREF_STR";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          tts = new TextToSpeech(this, null);
  
          prefN = 0;
          Button btnOk = (Button)findViewById(R.id.btnOk);
          txtText = (AutoCompleteTextView)findViewById(R.id.txtTesto);
          txtText.setThreshold(0);
  
           adapter = new ArrayAdapter&lt;String&gt;(
                  this,
                  android.R.layout.simple_dropdown_item_1line,
                  new String[] {}
          );
  
          txtText.setAdapter(adapter);
  
          btnOk.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  // Prendo stringa textBox
                  String str = txtText.getText().toString();
  
                  // Faccio parlare con il testo
                  tts.setLanguage(Locale.ITALIAN);
                  tts.speak(str, TextToSpeech.QUEUE_ADD, null);
  
                  // Stampo Toast
                  //Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
  
                  // Inserisco SharedPreference
                  SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, 0).edit().putString("p"+prefN, str);
                  editor.commit();
  
                  txtText.setText("");
  
                  combo();
              }
          });
      }
  
      public void combo()
      {
          SharedPreferences pref = getSharedPreferences(PREF_NAME, 0);
  
          adapter.add(pref.getString("p"+prefN, ""));
  
          adapter.getItem(0);
      }
  }
  </code></pre>
  
  <h3>logcat output</h3>
  
  <pre><code>01-12 12:10:18.683: ERROR/cutils-trace(1234): Error opening trace file: No such file or directory (2)
  01-12 12:10:47.164: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:47.164: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:47.164: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:47.164: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:47.173: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:47.173: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressStandard.ogg
  01-12 12:10:47.173: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressSpacebar.ogg
  01-12 12:10:47.183: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressDelete.ogg
  01-12 12:10:47.183: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressReturn.ogg
  01-12 12:10:49.543: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:49.553: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:49.553: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:49.563: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:49.563: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:49.563: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressStandard.ogg
  01-12 12:10:49.573: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressSpacebar.ogg
  01-12 12:10:49.573: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressDelete.ogg
  01-12 12:10:49.573: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressReturn.ogg
  01-12 12:10:56.163: ERROR/AndroidRuntime(1244): FATAL EXCEPTION: main
  java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
  at java.util.ArrayList.get(ArrayList.java:308)
  at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
  at com.vallauri.TTS.myTTSActivity.combo(myTTSActivity.java:76)
  at com.vallauri.TTS.myTTSActivity$1.onClick(myTTSActivity.java:65)
  at android.view.View.performClick(View.java:4240)
  at android.view.View$PerformClick.run(View.java:17721)
  at android.os.Handler.handleCallback(Handler.java:730)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5103)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)
  01-12 12:10:57.973: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:57.973: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:57.983: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:57.983: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:57.983: ERROR/SoundPool(297): error loading /system/media/audio/ui/Effect_Tick.ogg
  01-12 12:10:57.983: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressStandard.ogg
  01-12 12:10:57.993: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressSpacebar.ogg
  01-12 12:10:57.993: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressDelete.ogg
  01-12 12:10:58.003: ERROR/SoundPool(297): error loading /system/media/audio/ui/KeypressReturn.ogg
  01-12 12:10:58.153: ERROR/InputDispatcher(297): channel '41bacb50 com.vallauri.TTS/com.vallauri.TTS.myTTSActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
  </code></pre>