如何从Soap Web Service中填充spinner中的所有json数组?

时间:2016-07-05 11:22:16

标签: android json web-services spinner

以下是我的JSON响应

[
  {
    "msg":"4,MADHUVAN,5,AMBAMATA,6,PH-1,7,PH-2",
    "Value1":null,
    "Value2":null,
    "Value3":null,
    "Value4":null,
    "Value5":null,
    "Value6":null,
    "Value7":null,
    "Value8":null,
    "Value9":null,
    "Value10":null
  }
]

以下是我的Async_Task -

class async_Division extends AsyncTask<String, Void, String> {

  @Override
  protected String doInBackground(String... params) {

     SoapObject request = new SoapObject(namespacedivision, method_name__filldivision);
     request.addProperty(parameter_filldivision, circle);//add the parameters

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
     envelope.setOutputSoapObject(request);
     envelope.dotNet = true;
     try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(url_division);
        androidHttpTransport.call(soap_action_filldivision, envelope);  // this is the actual part that will call the webservice
        //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
        SoapObject response = (SoapObject) envelope.bodyIn;
        //resultstate = prim.toString();
        resultDivision = response.getPropertyAsString(0).toString();

        if (resultDivision != null) {
           try {

              JSONArray resultarray = new JSONArray(resultDivision);
              for (int i = 0; i < resultarray.length(); i++) {
                 JSONObject c = resultarray.getJSONObject(i);

                 String idLocality = c.getString("msg");
                 String nameLocality = (c.getString("msg"));

                 // Split data by comma
                 String[] namesList = idLocality.split(",");
                 String idDivision = namesList[0];
                 String nameDivision = namesList[1];
                 //cityname.add(cityName);

                 HashMap<String, String> hashlocality = new HashMap<String, String>();
                 hashlocality.put(TAG_ID_fillcombodivisionid, idDivision);
                 hashlocality.put(TAG_Name_fillcombodivisionname, nameDivision);
                 resultListDivision.add(hashlocality);
              }
           } catch (JSONException e) {
              e.printStackTrace();
           }
        } else {
           Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

     } catch (Exception e) {
        e.printStackTrace();
     }
     return resultDivision;

  }

  @Override
  protected void onPostExecute(String result1) {

     // TODO Auto-generated method stub

     SpinnerAdapter adapter = new SimpleAdapter(getActivity(), resultListDivision, R.layout.activity_showspinner_division, new String[]{TAG_ID_fillcombodivisionid, TAG_Name_fillcombodivisionname}, new int[]{R.id.textidlocality, R.id.textnamelocality});
       spnDivision.setAdapter(adapter);

     spnDivision.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
           // TODO Auto-generated method stub
           //save.setClickable(true);
           TextView txtid = (TextView) arg0.findViewById(R.id.textidlocality);
           division = txtid.getText().toString();
           // Call web service for district
           //callvillage();
           new async_Sub_Division().execute();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

     });

  }
  }

我在我的Spinner中填充了JSON响应。一切都很好但我的Spinner只显示一个值“MADHUVAN”。我想在我的JSONArray中显示所有Spinner,我也希望在零位置显示默认的硬编码文本,例如“select”。任何人都可以解决这个问题吗?

1 个答案:

答案 0 :(得分:-2)

更改你的for循环代码,它将起作用,

       for (int i = 0; i < resultarray.length(); i++) 
       {
             JSONObject c = resultarray.getJSONObject(i);

             String idLocality = c.getString("msg");

             // Split data by comma
             String[] namesList = idLocality.split(",");

             for(int i = 0; i < nameList.length; i+=2)
             {
                String idDivision = namesList[i];
                String nameDivision = namesList[i + 1];                 

                HashMap<String, String> hashlocality = new HashMap<String, String>();
                hashlocality.put(TAG_ID_fillcombodivisionid, idDivision);
                hashlocality.put(TAG_Name_fillcombodivisionname, nameDivision);
                resultListDivision.add(hashlocality);
             }                 
       }