下面是我的 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”。我想在我的 Spinner
中显示所有 JSONArray
我还想在零位置显示默认的硬编码文本,如“选择”。谁能解决这个问题?
最佳答案
在下面更改你的 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);
}
}
关于android - 如何从 Soap Web 服务填充微调器中的所有 json 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38202199/