title: how set selected spinner item in android studio?
body: <p>please help me..</p>
  
  <p>i want to set selected spinner item with same id..</p>
  
  <p>ok, in my account page i have many address book entries..</p>
  
  <p>and when i edit the address book..</p>
  
  <p>i pass the id of country and state from my database to my edit page..</p>
  
  <p>but my problem is i want to set selected item with same id of what i pass to edit page..</p>
  
  <p>here is the code that get the info from database..</p>
  
  <pre><code>public void getEditAddress(){
          class DownloadJSONEditAddress extends AsyncTask&lt;String, Void, String&gt; {
  
              @Override
              protected void onPreExecute() {
                  super.onPreExecute();
                  // Create a progressdialog
                  /*mProgressDialog = new ProgressDialog(getActivity());
                  // Set progressdialog title
                  //mProgressDialog.setTitle(cname);
                  // Set progressdialog message
                  mProgressDialog.setMessage("Please wait...");
                  mProgressDialog.setIndeterminate(false);
                  // Show progressdialog
                  mProgressDialog.show();*/
                  mProgressDialog = ProgressDialog.show(getActivity(), null, null, true, false);
                  mProgressDialog.setContentView(R.layout.progressdialog);
              }
  
              @Override
              protected String doInBackground(String... params) {
                  ArrayList&lt;NameValuePair&gt; dataToSend = new ArrayList&lt;&gt;();
                  dataToSend.add(new BasicNameValuePair("customersid", customersid));
                  dataToSend.add(new BasicNameValuePair("addressid", addressid));
  
                  HttpParams httpRequestParams = new BasicHttpParams();
                  HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
                  HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
  
                  DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                  HttpPost httppost = new HttpPost(SERVER_ADDRESS + "editaddress.php");
  
                  // Depends on your web service
                  //httppost.setHeader("Content-type", "application/json");
  
                  InputStream inputStream = null;
                  String editaddressinforesult = null;
                  try {
                      httppost.setEntity(new UrlEncodedFormEntity(dataToSend));
                      HttpResponse response = httpclient.execute(httppost);
                      HttpEntity entity = response.getEntity();
  
                      inputStream = entity.getContent();
                      // json is UTF-8 by default
                      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                      StringBuilder sb = new StringBuilder();
  
                      String line = null;
                      while ((line = reader.readLine()) != null)
                      {
                          sb.append(line + "\n");
                      }
                      editaddressinforesult = sb.toString();
                  } catch (Exception e) {
                      // Oops
                  }
                  finally {
                      try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                  }
                  return editaddressinforesult;
              }
  
              @Override
              protected void onPostExecute(String editaddressinforesult){
                  myJSONEditAddressInfo=editaddressinforesult;
  
                  try {
                      // Locate the array name in JSON
                      JSONObject jsonObjshippinginfo = new JSONObject(myJSONEditAddressInfo);
                      jsonarrayEditAddressInfo = jsonObjshippinginfo.getJSONArray("editaddressinfo");
                      for (int i = 0; i &lt; jsonarrayEditAddressInfo.length(); i++) {
                          JSONObject sp = jsonarrayEditAddressInfo.getJSONObject(i);
                          txtAddressId.setText(sp.getString("address_book_id"));
                          if(sp.getString("entry_gender").matches("m")){
                              radioMale.setChecked(true);
                              radioFemale.setChecked(false);
                          }else if(sp.getString("entry_gender").matches("f")){
                              radioMale.setChecked(false);
                              radioFemale.setChecked(true);
                          }
                          txtFname.setText(sp.getString("entry_firstname"));
                          txtLname.setText(sp.getString("entry_lastname"));
                          txtCompanyName.setText(sp.getString("entry_company"));
                          txtStreetAddress.setText(sp.getString("entry_street_address"));
                          txtAddressLine2.setText(sp.getString("entry_suburb"));
                          txtCity.setText(sp.getString("entry_city"));
                          txtPostZipCode.setText(sp.getString("entry_zip"));
                          txtNoState.setText(sp.getString("entry_state"));
                          txtCountryid.setText(sp.getString("countries_id"));
                          txtStateid.setText(sp.getString("entry_zone_id"));
  
                          txtDBCountryid.setText(sp.getString("countries_id"));
                          txtDBStateid.setText(sp.getString("entry_zone_id"));
  
                          if(sp.getString("entry_shipping_billing").matches("1")){
                              chkSetPrimary.setChecked(true);
                          }
  
                          getCountries(sp.getString("countries_id"), sp.getString("entry_zone_id"));
                      }
                  } catch (JSONException e) {
                      Log.e("Error", e.getMessage());
                      e.printStackTrace();
                  }
                  // Close the progressdialog
                  //mProgressDialog.dismiss();
              }
          }
          DownloadJSONEditAddress g = new DownloadJSONEditAddress();
          g.execute();
      }
  </code></pre>
  
  <p>and in this part of the code..</p>
  
  <pre><code>getCountries(sp.getString("countries_id"), sp.getString("entry_zone_id"));
  </code></pre>
  
  <p>i pass <code>countries_id</code> and <code>entry_zone_id</code></p>
  
  <p>now in my <code>getCountries</code> i want set selected item with same <code>countries_id</code></p>
  
  <pre><code>public void getCountries(final String gcountries_id, final String gentry_zone_id) {
          class DownloadcountriesJSON extends AsyncTask&lt;String, Void, String&gt; {
  
              @Override
              protected void onPreExecute() {
                  super.onPreExecute();
                  // Create a progressdialog
                  /*mProgressDialog = new ProgressDialog(getActivity());
                  // Set progressdialog title
                  //mProgressDialog.setTitle(cname);
                  // Set progressdialog message
                  //mProgressDialog.setMessage("Please wait...");
                  mProgressDialog.setIndeterminate(false);
                  // Show progressdialog
                  mProgressDialog.show();*/
                  /*mProgressDialog = ProgressDialog.show(getActivity(), null, null, true, false);
                  mProgressDialog.setContentView(R.layout.progressdialog);*/
              }
  
              @Override
              protected String doInBackground(String... params) {
                  HttpParams httpRequestParams = new BasicHttpParams();
                  HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
                  HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
  
                  DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                  HttpPost httppost = new HttpPost(SERVER_ADDRESS + "countries.php");
  
                  // Depends on your web service
                  //httppost.setHeader("Content-type", "application/json");
  
                  InputStream inputStream = null;
                  String result = null;
                  try {
                      HttpResponse response = httpclient.execute(httppost);
                      HttpEntity entity = response.getEntity();
  
                      inputStream = entity.getContent();
                      // json is UTF-8 by default
                      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                      StringBuilder sb = new StringBuilder();
  
                      String line = null;
                      while ((line = reader.readLine()) != null) {
                          sb.append(line + "\n");
                      }
                      result = sb.toString();
                  } catch (Exception e) {
                      // Oops
                  } finally {
                      try {
                          if (inputStream != null) inputStream.close();
                      } catch (Exception squish) {
                      }
                  }
                  return result;
              }
  
              @Override
              protected void onPostExecute(String result) {
                  registerJSON = result;
                  countrylist = new ArrayList&lt;String&gt;();
                  country = new ArrayList&lt;CountryId&gt;();
                  try {
                      // Locate the array name in JSON
                      JSONObject regjsonObj = new JSONObject(result);
                      regjsonarray = regjsonObj.getJSONArray("countries");
  
                      //countrylist.add("Country");
                      for (int i = 0; i &lt; regjsonarray.length(); i++) {
                          regjsonobject = regjsonarray.getJSONObject(i);
  
  
                          CountryId countryid = new CountryId();
                          countryid.setCountryid(regjsonobject.optString("countries_id"));
                          country.add(countryid);
  
  
                          countrylist.add(regjsonobject.optString("countries_name"));
                      }
  
                      spinnerCountry.setAdapter(new ArrayAdapter&lt;String&gt;(getActivity(),
                              R.layout.spinner_item,
                              countrylist));
  
                      // Spinner on item click listener
                      spinnerCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  
                          @Override
                          public void onItemSelected(AdapterView&lt;?&gt; arg0,
                                                     View arg1, int position, long arg3) {
                              getStates(country.get(position).getCountryid());
                          }
  
                          @Override
                          public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                          }
                      });
  
                  } catch (JSONException e) {
                      Log.e("Error", e.getMessage());
                      e.printStackTrace();
                  }
  
                  //mProgressDialog.dismiss();
              }
          }
          DownloadcountriesJSON g = new DownloadcountriesJSON();
          g.execute();
      }
  </code></pre>
  
  <p>please help me..</p>
  
  <p>im new to android..</p>
  
  <p>sorry for my bad english.. :)</p>
  
  <p>thanks,</p>
  
  <p>Joe</p>