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