0

在这段代码中,我正在使用 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();
         }


      }

} }

4

2 回答 2

0

Android ArrayAdapter 会创建自己的 allItems 数据数组的副本,因此通过 AsyncTask 更改此数组后,您必须更新适配器,您可以使用

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

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

于 2014-04-05T07:28:36.370 回答
0

在您执行了异步任务之后,您还没有通知适配器您的 ArrayList 中有更改。紧随其后

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

添加这些行:

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

这应该适合你。

于 2014-04-05T07:47:48.687 回答