从webservice动态加载数据后,所选项目不会显示在微调器中

时间:2014-04-05 07:18:34

标签: android android-spinner android-webservice

我这个代码我正在使用Web服务将数据动态加载到微调器。并且数据正确加载到其中。但是当我单击微调器选择一个项目时,所选项目不会显示在微调器中。

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();
         }


      }

} }

2 个答案:

答案 0 :(得分:0)

Android ArrayAdapter使其成为allItems数据数组的副本,因此在通过AsyncTask更改此数组后,您必须更新适配器,您可以使用

执行此操作
dataAdapter.clear();
dataAsapter.addAll(allNames);

您可以将这些代码行放在onPostExecute

答案 1 :(得分:0)

执行asyntask之后,您还没有通知适配器您的ArrayList中有更改。只需在

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

添加以下行:

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

这对你有用。