如何使用android中的异步任务从webservice加载后在spinner中填充数据

时间:2014-05-31 10:14:02

标签: java android web-services android-asynctask android-spinner

我想在状态数组来自webService之后填充android spinner中的数据。加载进度对话框后,它将在微调器中显示数据。

以下是我的代码。我在doInBackground方法中遇到错误。我得到了正确的网络服务响应。但我不会在微调器中显示数据。

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SpinnerExample extends Fragment {

    private static ProgressDialog pleaseWaitDialog;
    private static ProgressDialog pDialog;
    private View rootView;
    private Spinner state_spinner;
    private String[] statesArray;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.spinner, container, false);
        state_spinner = (Spinner) rootView.findViewById(R.id.state_spinner);
        SoapAccessTask soapAccessTask = new SoapAccessTask();
        soapAccessTask.execute();
        return rootView;
    }

    private class SoapAccessTask extends AsyncTask<String, Void, String> {
        // Pre execution actions
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading State List");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
            statesArray = WebService.getStateList();
            return statesArray;
        }

        @Override
        protected void onPostExecute(String result) {
            if (statesArray != null) {
                ArrayAdapter<String> stateAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, statesArray);
                stateAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                state_spinner.setAdapter(stateAdapter);
                pleaseWaitDialog.dismiss();
            } else {
                AlertDialogError.showMessage(getActivity(), "Server Error");
            }
        }
    }
}

0 个答案:

没有答案