我遇到了一些错误。我知道问题出在哪里,但我不知道如何解决。
我有 4 个微调器。每个微调器都关联一个异步任务。简而言之,当我在 spinner1 上选择一个项目时,执行填充 spinner2 的第二个 asynctask(所选项目作为参数添加到第二个 asynctask)以这种方式继续。
但是在第一个异步任务结束之前,spinner2 无法填充。在这种情况下,我遇到了错误。如何解决?如何让第一个任务完成,第二个任务工作?
这是代码;
第一项任务
public class ilceDoldur extends AsyncTask<String[],String[],String[]>{
@Override
protected String[] doInBackground(String[]... params) {
SoapObject request = new SoapObject(NAMESPACE, "ILCEGetir");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/ILCEGetir", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
ilceList = new String[response.getPropertyCount()];
for(int i=0;i<response.getPropertyCount();i++){
ilceList[i] = response.getPropertyAsString(i).toString().trim();
}
}
catch (Exception e) {
e.printStackTrace();
}
return ilceList;
}
protected void onPostExecute(String[] ilceList){
ilceSpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> ilce_adapter = new ArrayAdapter<String>(NormalAnasayfa.this,android.R.layout.simple_spinner_item, ilceList);
ilce_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ilceSpinner.setAdapter(ilce_adapter);
kontrol=true;
}
}
第二个任务
public class semtDoldur extends AsyncTask<String,String[],String[]>{
@Override
protected String[] doInBackground(String... ParamIlce) {
PropertyInfo ilce = new PropertyInfo();
ilce.name= "ilce";
ilce.setValue(ParamIlce);
ilce.type = PropertyInfo.STRING_CLASS;
SoapObject request = new SoapObject(NAMESPACE, "SEMTGetir");
request.addProperty(ilce);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/SEMTGetir", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
semtList = new String[response.getPropertyCount()];
for(int i=0;i<response.getPropertyCount();i++){
semtList[i] = response.getPropertyAsString(i).toString().trim();
}
}
catch (Exception e) {
e.printStackTrace();
}
return semtList;
}
protected void onPostExecute(String[] semtList){
semtSpinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> semt_adapter = new ArrayAdapter<String>(NormalAnasayfa.this, android.R.layout.simple_spinner_item, semtList);
semt_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
semtSpinner.setAdapter(semt_adapter);
kontrol=true;
}
}
onCreate()
new ilceDoldur().execute();
if(kontrol==true){
kontrol=false;
ilceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
secilenIlce = ilceSpinner.getSelectedItem().toString().trim();
new semtDoldur().execute(secilenIlce);
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
编辑:我从我的网络服务获取数据并填充 spinner1。我将在哪里获得数据(spinner1 所选项目)?我在哪里填充 spinner2 执行第二个任务?