title: How Show id and Name to android Spinner?
body: <p>Good day, now i'm able to show a spinner from my database table, here is how i do it </p>
  
  <pre><code>  @Override
      public void onStart() {
          super.onStart();
          BackTask bt=new BackTask();
          bt.execute();
          getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
      }
  
       @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
  
              ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.addticket_layout, container, false);
  
              sp = rootView.findViewById(R.id.masalahval);
  
              adapter= new ArrayAdapter&lt;String&gt;(getActivity(),R.layout.spinner_layout,R.id.txt,problemId);
              sp.setAdapter(adapter);
  
              Button uploadButton = rootView.findViewById(R.id.uploadButton);
              Button saveTicket   = rootView.findViewById(R.id.saveTicket);
  
              uploadButton.setOnClickListener(this);
              saveTicket.setOnClickListener(this);
  
              return rootView;
          }
  </code></pre>
  
  <p>and here is the <code>asynctask</code></p>
  
  <pre><code>private class BackTask extends AsyncTask&lt;Void,Void,Void&gt; {
      ArrayList&lt;String&gt; list;
      String result="";
      protected void onPreExecute(){
          super.onPreExecute();
          list=new ArrayList&lt;&gt;();
      }
      protected Void doInBackground(Void... params) {
          try {
              url = new URL("http://192.168.3.223:84/my_api/GenerateProblemlists");
  
          } catch (MalformedURLException e) {
              e.printStackTrace();
          }
  
          try {
              // Setup HttpURLConnection class to send and receive data from php and mysql
              conn = (HttpURLConnection)url.openConnection();
              conn.setConnectTimeout(100);
              conn.setRequestMethod("POST");
              // setDoInput and setDoOutput method depict handling of both send and receive
              conn.setDoInput(true);
              conn.setDoOutput(true);
              // Append parameters to URL
              Uri.Builder builder = new Uri.Builder();
  
              OutputStream os = conn.getOutputStream();
  
              os.close();
              conn.connect();
  
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          try {
  
              int response_code = conn.getResponseCode();
  
              if (response_code == HttpURLConnection.HTTP_OK) {
                  InputStream input = conn.getInputStream();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                      result += line;
                  }
  
                  try{
                      JSONArray jArray =new JSONArray(result);
  
                      for(int i=0;i&lt;jArray.length();i++){
                          JSONObject jsonObject=jArray.getJSONObject(i);
  
                          String ProblemCode = jsonObject.getString("ProblemCode");
  
                          Log.e("URUT " + i,  "Data" + ProblemCode);
  
                           list.add(ProblemCode);
                      }
  
                  }
                  catch(JSONException e){
                      Log.e("Tag",   "Err" + e );
                       e.printStackTrace();
                  }
  
              }else{
                  Toast.makeText(getActivity(), "It's a bug !!!!", Toast.LENGTH_SHORT).show();
              }
  
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              conn.disconnect();
          }
  
          return null;
      }
      protected void onPostExecute(Void result){
          problemId.addAll(list);
          adapter.notifyDataSetChanged();
          //Log.d("Tag 1" , "OnPostExecute " + result);
  
      }
  }
  </code></pre>
  
  <p>So, as you can see from my script above i do this in <code>doInBackground()</code></p>
  
  <pre><code>list.add(ProblemCode);
  </code></pre>
  
  <p>And then here is my spinner result </p>
  
  <p><a href="https://i.stack.imgur.com/P4Qtv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P4Qtv.png" alt="enter image description here"></a></p>
  
  <p>Because that field is <code>ProblemCode</code> so it will showing the <code>Id</code> from table. But now i want to do something like this. My spinner will show the <code>Description</code> but when i get choose on of them i can get the <code>ProblemId</code>. </p>
  
  <p>I'm trying to change my code like this </p>
  
  <pre><code> JSONArray jArray =new JSONArray(result);
  
                      for(int i=0;i&lt;jArray.length();i++){
                          JSONObject jsonObject=jArray.getJSONObject(i);
  
                          String ProblemCode = jsonObject.getString("ProblemCode");
                          String ProblemDesc = jsonObject.getString("ProblemDesc");
  
                          Log.e("URUT " + i,  "Data" + ProblemCode);
  
                           list.add(ProblemCode + " -- " +  ProblemDesc);
                      }
  </code></pre>
  
  <p>and my spinner became like this</p>
  
  <p><a href="https://i.stack.imgur.com/dwgg9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dwgg9.png" alt="enter image description here"></a></p>
  
  <p>So, in my newest spinner image i'm only want to show description only, when i choose one of them i can get the <code>id</code> . How can i achieve that ? thanks in advance and sorry for my english</p>
  
  <p>Update --</p>
  
  <p>After i use <code>Dileep Patel</code> way i get this error</p>
  
  <blockquote>
    <p>01-30 13:34:47.825 6971-6971/xxx.example.boby.helpdesk
    E/AndroidRuntime: FATAL EXCEPTION: main</p>
    
    <blockquote>
      <p>Process: com.example.xxx.helpdesk, PID: 6971
                                                                           java.lang.NullPointerException
                                                                               at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
                                                                               at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:114)
                                                                               at android.widget.Spinner.setAdapter(Spinner.java:413)
                                                                               at
      android.support.v7.widget.AppCompatSpinner.setAdapter(AppCompatSpinner.java:399)
                                                                               at com.example.boby.helpdesk.AddTicket.onCreateView(AddTicket.java:99)
                                                                               at android.app.Fragment.performCreateView(Fragment.java:1700)
                                                                               at
      android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
                                                                               at
      android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
                                                                               at android.app.BackStackRecord.run(BackStackRecord.java:684)
                                                                               at
      android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
                                                                               at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
                                                                               at android.os.Handler.handleCallback(Handler.java:733)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:136)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5019)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                                               at
      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                               at dalvik.system.NativeStart.main(Native Method)</p>
    </blockquote>
  </blockquote>