title: Unable to add hint in android spinner
body: <p>I have a <code>spinner</code> that behaves like a <code>dropdown</code> in my <code>android application</code>. I am developing the app in <code>android studio</code>. The spinner get data from the <code>api</code>. At run time it gets <code>usernames</code> from <code>API</code> and shows in the <code>spinner</code>. When app runs it shows the <code>username</code> and <code>ID</code> of the selected user like shown in below image</p>
  
  <p><a href="https://i.stack.imgur.com/8Svgom.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Svgom.png" alt="enter image description here"></a></p>
  
  <p>Now i want to add hint so i searched many articles and found many solutions and i follow the easiest of them. For more understanding please see the below code</p>
  
  <pre><code> try
          {
              JSONObject jobj = new JSONObject(jsonObject.toString());
              // Locate the NodeList name
              jsonArray = jobj.getJSONArray("users");
  
              for(int i=0; i&lt;jsonArray.length(); i++)
              {
                  jsonObject = jsonArray.getJSONObject(i);
  
                  Users user = new Users();
  
                  user.setId(jsonObject.optString("Id"));
                  user.setName(jsonObject.optString("Name"));
                  users.add(user);
  
                  userList.add("Select a username");// i add this as a hint
                  userList.add(jsonObject.optString("Name"));
  
              }
          } catch (JSONException e) {
              Log.e("Error", e.getMessage());
              e.printStackTrace();
          }
  </code></pre>
  
  <p>Now on <strong>onPostExecute()</strong> method </p>
  
  <pre><code>  @Override
      protected void onPostExecute(Void args)
      {
          // Locate the spinner in activity_main.xml
          Spinner spinner = (Spinner)findViewById(R.id.spinner);
  
          // Spinner adapter
          spinner.setAdapter(new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, userList));
  
          // Spinner on item click listener
  
          spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
                  textViewResult = (TextView)findViewById(R.id.textView);
  
                  // Set the text followed by the position
  
                  // Set the text followed by the position
                  if(!users.get(position).getName().equals("Select a username")) {
                      textViewResult.setText(" " + users.get(position - 1).getName() + "  " + users.get(position - 1).getId());
                  }else {
  
                      textViewResult.setText("Hi " + users.get(position).getName() + " your ID is " + users.get(position).getId());
                      UserId = String.valueOf(users.get(position).getId());
                      progressDialog.dismiss();
                      _latitude.setText("");
                      _longitude.setText("");
                      Latitude = null;
                      Longitude = null;
  
                  }
  
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
                  textViewResult.setText("");
              }
          });
      }
  </code></pre>
  
  <p>When i run the application the app crashes while giving me the below error </p>
  
  <pre><code>Process: com.example.accurat.myapp, PID: 30382
                                                                         java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
                                                                             at java.util.ArrayList.get(ArrayList.java:310)
                                                                             at com.example.accurat.myapp.MainActivity$DownloadJSON$1.onItemSelected(MainActivity.java:494)
                                                                             at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
                                                                             at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
                                                                             at android.widget.AdapterView.-wrap1(AdapterView.java)
                                                                             at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
                                                                             at android.os.Handler.handleCallback(Handler.java:746)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5491)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  </code></pre>
  
  <p>This error hits at the point <code>textViewResult.setText(" " + users.get(position - 1).getName() + "  " + users.get(position - 1).getId());</code></p>
  
  <p><strong>Update 1</strong></p>
  
  <p>following the answer of <code>Junaid Hafeez</code> i have done the following </p>
  
  <pre><code>for(int i=0; i&lt;jsonArray.length(); i++)
              {
                  jsonObject = jsonArray.getJSONObject(i);
  
                  Users user = new Users();
  
                  user.setId(jsonObject.optString("Id"));
                  user.setName(jsonObject.optString("Name"));
                  users.add(user);
  
  
                  userList.add(jsonObject.optString("Name"));
  
              }
              userList.add(0, "Select a username"); // after for loop ended i add `select a username` at `0` index 
  </code></pre>
  
  <p>after that in <code>postExecute()</code> method i have done the following </p>
  
  <pre><code> @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
                  textViewResult = (TextView)findViewById(R.id.textView);
  
                  // Set the text followed by the position
  
                  // Set the text followed by the position
                  if(position&gt;0)
                  {
                      textViewResult.setText("Hi " + users.get(position).getName() + " your ID is " + users.get(position).getId());
  
                      UserId = String.valueOf(users.get(position).getId());
  
                      _latitude.setText("");
                      _longitude.setText("");
                      Latitude = null;
                      Longitude = null;
                  }
                  else {
  
  
                  }
  
                  progressDialog.dismiss();
  
              }
  </code></pre>
  
  <p>The result i get is below </p>
  
  <p><a href="https://i.stack.imgur.com/5DAn1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5DAn1.png" alt="enter image description here"></a></p>
  
  <p>It does show me the hint, but when i select any username it shows me the <code>name</code> and <code>id</code> of the next username as shown in below image </p>
  
  <p><a href="https://i.stack.imgur.com/6AQVy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6AQVy.png" alt="enter image description here"></a></p>
  
  <p>I am stuck to it and couldn't find any solution </p>
  
  <p>Any help would be highly appreciated.</p>