0

I this code I am using web service to load data dynamically to the spinner. And data is properly loaded in it. But when I click on spinner to select an item the selected item does not displayed in spinner.

public class MainActivity extends Activity 
{
    TextView webserviceResponse;
    private Button btnCheck;
    public String set;

    private ProgressDialog prgDialog;
    public static final int progress_bar_type = 0;    

    List<String> allNames=new ArrayList<String>();
    private Spinner spinCityName;

    ArrayAdapter<String> dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        spinCityName=(Spinner)findViewById(R.id.spinCity);

         dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,allNames);
         dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         dataAdapter.notifyDataSetChanged();

        spinCityName.setAdapter(dataAdapter);


             new temp().execute("GetCities");
    }


        @Override
    protected Dialog onCreateDialog(int id) 
    {
        switch (id) 
        {
        case progress_bar_type:
            prgDialog = new ProgressDialog(this);
            prgDialog.setMessage("Please wait..");
            prgDialog.setIndeterminate(false);
            prgDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            prgDialog.setCancelable(false);
            prgDialog.show();
            return prgDialog;

            default:
                return null;
        }


    }

        class temp extends AsyncTask<String, String, String> 
        {

                String namespace = "http://tempuri.org/";
                private String url = "http://ds.initqube.com/DocServe.asmx";

                String SOAP_ACTION;
                SoapObject request = null, objMessages = null;
                SoapSerializationEnvelope envelope;
                HttpTransportSE androidHttpSE;



                protected void SetEnvelope() 
                {

                    try 
                    {
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);

                    // Creating SOAP envelope           
                        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                        //You can comment that line if your web service is not .NET one.
                        envelope.dotNet = true;

                        envelope.setOutputSoapObject(request);
                        androidHttpSE = new HttpTransportSE(url);


                    }
                    catch (Exception e) 
                    {
                        System.out.println("Soap Exception---->>>" + e.toString());    
                    }
                }



                @SuppressWarnings("deprecation")
                @Override
                protected void onPreExecute() 
                {
                    super.onPreExecute();

                    showDialog(progress_bar_type);

                }




            @Override
            protected String doInBackground(String...f_url)
            {
                String jsonStr=null;
                if(f_url[0]=="GetCities")
                {
                    jsonStr=LoadCities(f_url[0]);


                }

                return jsonStr;
             }



            protected void onProgressUpdate(String... progress) {

                prgDialog.setProgress(Integer.parseInt(progress[0]));
            }


            // Once Music File is downloaded
            @Override
            protected void onPostExecute(String result) 
            {

                dismissDialog(progress_bar_type);

                if (result != null) 
                {
                    try {
                            JSONArray array = new JSONArray(result);

                            for(int i=0;i<array.length();i++)
                            {
                                    JSONObject jObj = array.optJSONObject(i);
                                    String cityName = jObj.optString("CityName");
                                    int CityId = jObj.optInt("CityId");

                                    allNames.add(cityName);
                                    Log.v("cityName :",""+cityName);
                                    Log.v("CityId :",""+CityId);
                            }
                        }

                        catch(JSONException e)
                        {
                            e.printStackTrace();
                        }
                }

                else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            }


        // To load cities
        protected  String LoadCities(String methodName)
         {
            String result;
            try {   

                SOAP_ACTION = namespace + methodName;

                //Adding values to request object
                request = new SoapObject(namespace,methodName);


                SetEnvelope();

                try {

                        //SOAP calling webservice
                        androidHttpSE.call(SOAP_ACTION, envelope);

                        //Got Webservice response
                        result = envelope.getResponse().toString();

                        //return result;
                        //onPostExecute(result);
                        return result;
                    } 

                    catch (Exception e) 
                    {
                        // TODO: handle exception
                        return e.toString();
                    }
         } 
         catch (Exception e) 
         {
                // TODO: handle exception
                return e.toString();
         }


      }

} }

Pravesh
  • 822
  • 1
  • 8
  • 20

2 Answers2

0

Android ArrayAdapter makes it's own copy of your allItems data array, so after change this array through AsyncTask, you must update your adapter, you can do this by use of

dataAdapter.clear();
dataAsapter.addAll(allNames);

you can place these lines of code in onPostExecute

0

After you have executed your asyntask, you haven't notified the adapter that the there are changes in your ArrayList. simply after

new temp().execute("GetCities");

add these lines:

dataAdapter.addAll(allNames);
dataAdapter.notifyDataSetChanged();

this should work for you.

gkapagunta
  • 188
  • 1
  • 11