我有一些错误。我知道是什么问题,但我不知道如何解决它。
我有 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;
}
}
在创建()
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) {}
});
}
编辑:
我从我的 Web 服务获取数据并填充 spinner1。我将在哪里获得数据(spinner1 所选项目)?我在哪里填充 spinner2 执行第二个任务?
最佳答案
我找到解决方案!
从网络服务获取数据:
private void ilceDoldur(){
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();
}
}
我的异步任务:
public class ilceDoldur extends AsyncTask<String,String[],String[]>{
@Override
protected String[] doInBackground(String... params) {
ilceDoldur();
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);
ilceSecildi();
}
}
获取 spinner1 所选项目并启动第二个异步任务:
public String ilceSecildi(){
ilceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
secilenIlce = ilceSpinner.getSelectedItem().toString().trim();
new semtDoldurAsync(secilenIlce).execute();
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
return secilenIlce;
}
在 onCreate 中执行第一个任务:
new ilceDoldur().execute();
关于android - Android Spinner 上的多个 AsyncTasks 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11878599/