Android上的微调多AsyncTasks问题

Android上的微调多AsyncTasks问题

本文介绍了Android上的微调多AsyncTasks问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一些错误。我知道是什么问题,但我不知道如何解决它。

I'm getting some error. I know what is the problem but I don't know how to fix it.

我有4微调。每个微调相关的一条AsyncTask的。对于短,当我选择spinner1一个项目,以这种方式执行该填充spinner2(选择项添加到第二个的AsyncTask作为参数)第二AsyncTask的继续。

I've 4 spinner. Every spinner related one asynctask. For short, When I select one item on spinner1 , execute second asynctask that populate spinner2 (selected item add to second asynctask as parameter) Continues in this way.

但首先的AsyncTask结束之前,spinner2无法填充。在这种情况下,我得到的错误。如何解决呢?如何使当第一个任务完成,第二个任务的工作?

But before end of first asynctask, spinner2 can't populate. In this case I'm getting error. How to fix it ? How to make when first task finish, second task work ?

下面是code;

首要任务

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()

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执行第二个任务?

推荐答案

我找到的解决方案!

得到web服务的数据:

get data from web service:

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:

my 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的:

Get spinner1 selected item and start second 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第一个任务:

execute first task in onCreate:

new ilceDoldur().execute();

这篇关于Android上的微调多AsyncTasks问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:17