我收到了一些错误。我知道问题是什么,但我不知道如何解决它。
我有4个旋转器。每个微调器都有一个asynctask。简而言之,当我在spinner1上选择一个项目时,执行填充spinner2的第二个asynctask(选择的项目添加到第二个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) {}
});
}
修改: 我从我的Web服务获取数据并填充spinner1。我会得到数据(spinner1选择项目)?我在哪里填充spinner2执行第二个任务?
答案 0 :(得分:0)
我找到了解决方案!
从网络服务获取数据:
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();
}
}
我的AsyncTask:
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选择的项目并启动第二个asynctask:
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();
答案 1 :(得分:0)
根据我对这个问题的理解,你说你有多个AsyncTasks来控制多个Spinners的内容。你做了一些交互,导致Spinner1被填充并立即进行另一个应该导致Spinner2被填充的交互,但观察到的行为是Spinner2在Spinner1完成填充之前不会填充。
这是因为AsyncTasks的默认行为是进程中的所有进程串行执行。如果希望AsyncTasks并行运行,请使用AsyncTask.executeOnExecutor()并指定线程池执行程序。尽管如此,请仔细阅读AsyncTask的文档,默认的并行性行为随着时间的推移而发生了变化。
答案 2 :(得分:0)
只需更改onPostExecute
即可 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();
}
}
到
protected void onPostExecute(String[] ilceList){
ilceSpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> ilce_adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, ilceList);
ilce_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ilceSpinner.setAdapter(ilce_adapter);
ilceSecildi();
}
}