0

我正在开发一个应用程序,我需要从一个中填充微调器,webservice并且还存在第二个微调器,它们在选择第一个微调器项目后填充。第二(微调器 B)取决于微调器 A。我得到了 A 的列表,但 b 微调器有问题。

我的基本要求是当我选择微调器“A”的项目时,它将加载微调器“b”的所有项目 这里是代码::

package com.CaribPay;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.Setting.CustomParser;

public class Register2 extends Activity implements OnItemClickListener {
    private Button BtnReg2Next;
    private EditText EdtxReg2Streetadd1,
                     EdtxReg2Streetadd2,
                     EdtxReg2City,
                     EdtxReg2Zipcode;

    private Spinner 
                    SpnrReg2Contry,
                    SpnrReg2Agent,
                    SpnrReg2State,
                    SpnrReg2PrimaryCurr,
                    SpnrReg2SecondaryCurr;

    private CustomParser cstParsr = new CustomParser();

    private String[] Regstrationstep1;
    private String[] strCountry,strProvince;
    private String[] Primarysecondarycur;
    private int globPosition;
    private String strSelecteccontry;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register2);
        GetDataFromLastPage();

        new FillSpinnerTask().execute();



    }

      private class FillSpinnerTask  extends AsyncTask<Void, Void, Void> {
            private ProgressDialog dialog = new ProgressDialog(
                    Register2.this);

            @Override
            protected void onPreExecute() {
                this.dialog.setMessage(getString(R.string.MsgPleasewait));
                this.dialog.show();
                this.dialog.setCancelable(false);
                // put your code which preload with processDialog

            }

            @Override
            protected Void doInBackground(Void... arg0) {
                // put your code here
                strCountry = cstParsr.LoadCountriesForMobApp(getApplicationContext());

                return null;
            }

            @Override
            protected void onPostExecute(final Void unused) {
                if (this.dialog.isShowing()) {
                    try {
                        this.dialog.dismiss();
                        this.dialog = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if(strCountry != null)
                    {
                        for(int i=0;i<strCountry.length;i++)
                        {
                            Log.i("TAG","strCountry"+strCountry[i]);
                        }

                    }
                    FillView();

                }
            }
        }


    private void FillView() {

        BtnReg2Next = (Button)findViewById(R.id.BtnReg2Next);
        EdtxReg2Streetadd1 = (EditText)findViewById(R.id.EdtxReg2Streetadd1);
        EdtxReg2Streetadd2= (EditText)findViewById(R.id.EdtxReg2Streetadd2);
        EdtxReg2City  = (EditText)findViewById(R.id.EdtxReg2City);
        EdtxReg2Zipcode =  (EditText)findViewById(R.id.EdtxReg2Zipcode);

        SpnrReg2Contry = (Spinner)findViewById(R.id.SpnrReg2Contry); 
        SpnrReg2Agent = (Spinner)findViewById(R.id.SpnrReg2Agent);
        SpnrReg2State = (Spinner)findViewById(R.id.SpnrReg2State);
        SpnrReg2PrimaryCurr = (Spinner)findViewById(R.id.SpnrReg2PrimaryCurr);
        SpnrReg2SecondaryCurr= (Spinner)findViewById(R.id.SpnrReg2SecondaryCurr);


        if(strCountry != null)
        {
            ArrayAdapter languagelist = new ArrayAdapter(
                Register2.this,
                android.R.layout.simple_spinner_item, 
                strCountry);

            languagelist.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
            SpnrReg2Contry.setAdapter(languagelist);
        }


        SpnrReg2Contry.setOnItemSelectedListener(new OnItemSelectedListener() 
           {
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long arg3) 
                {   

                        strSelecteccontry = strCountry[position];
                        new FillProvinceSpinnerTask().execute();

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }


            });

        /*SpnrReg2State.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(strProvince == null)
                {
                new FillProvinceSpinnerTask().execute();
                }
                return true;
            }
        });*/



        BtnReg2Next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(EdtxReg2Streetadd1.getText().toString().equalsIgnoreCase("")|
                     EdtxReg2Streetadd2.getText().toString().equalsIgnoreCase("")|
                     EdtxReg2City.getText().toString().equalsIgnoreCase("")|
                     EdtxReg2Zipcode.getText().toString().equalsIgnoreCase(""))
                {
                    Toast.makeText(getApplication(),getString(R.string.nullmessage),Toast.LENGTH_LONG).show();
                }else
                {
                     Intent Registration2 = new Intent(Register2.this,Register3.class);
                     startActivity(Registration2);
                     finish();
                }


            }
        });
    }

    private void GetDataFromLastPage() {
        // TODO Auto-generated method stub
        Intent intent = getIntent();

        Regstrationstep1 = new String[9];
        Regstrationstep1 = intent.getStringArrayExtra("Regstrationstep1");


    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int Pos, long arg3) {
        // TODO Auto-generated method stub
    }

    private class FillProvinceSpinnerTask  extends AsyncTask<Void, Void, Void> {
        private ProgressDialog dialog = new ProgressDialog(
                Register2.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage(getString(R.string.MsgPleasewait));
            this.dialog.show();
            this.dialog.setCancelable(false);
            // put your code which preload with processDialog

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // put your code here
            strProvince = cstParsr.LoadProvinceFromCountryForMobApp(getApplicationContext(),strSelecteccontry);


            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                try {
                    this.dialog.dismiss();
                    this.dialog = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if(strProvince != null)
                {


                    Primarysecondarycur = strProvince[0].split("~");
                    strProvince[0] = "Please Select a State";
                ArrayAdapter languagelist = new ArrayAdapter(
                        Register2.this,
                        android.R.layout.simple_spinner_item, 
                        strProvince);

                languagelist.setDropDownViewResource(
                   android.R.layout.simple_spinner_dropdown_item);
                SpnrReg2State.setAdapter(languagelist);


                }
            }
        }
    }


}
4

1 回答 1

1

OnItemSelectedListener 的问题在于它在声明时选择了第一个值。为了避免第一次调用,请执行以下操作:

    int count = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
        ...
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                if(count>0) {
                    // do what you want on item selection
                } else {
                    count++;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }
于 2012-08-09T06:47:01.727 回答