根据条件填充微调器

时间:2014-07-01 10:39:18

标签: android android-asynctask android-spinner

我创建了一个注册表单,其中我有2个微调器(State,Area)和AutoTextView city。当用户选择一个特定的状态时,来自该状态的相应引用应该只在该微调器AutoTextView中填充。对于那个我已经创建和asynck任务并以json的形式获取引用。但我的问题是后台任务在用户选择State之前启动,所以我总是得到状态的第一个值。

代码

public void onClick(View view) {

        str_first_name = first_name.getText().toString();
        str_last_name = last_name.getText().toString();
        str_mobile = mobile.getText().toString();
        str_city = city.getText().toString();
        str_area = area.getText().toString();





        //for state
        str_state = (String) state.getSelectedItem();
        state_short = getResources().getStringArray(R.array.State_Short);
        state_code = state_short[state.getSelectedItemPosition()];
        state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               stateFull = (String) state.getSelectedItem();
                Log.e("State selected",""+stateFull);
                getCity getCity = new getCity();
                getCity.execute(stateFull);
            }

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

            }
        });


        //for address
        str_address = address.getText().toString();



        switch (view.getId()) {

            case R.id.bt_continue_1:


                if (str_first_name.equals("") || str_last_name.equals("") || str_gender.equals("Select Gender") || str_religion.equals("Select Religion") || str_age.equals("Select Age")) {
                    commonFunctions.showAlert(this, "INVALID", "Please fill all the mandatory details");
                    personal_info.setVisibility(view.VISIBLE);
                    professional_info.setVisibility(view.GONE);
                    contact_info.setVisibility(view.GONE);
                } else {

                    header.setText("Contact Info");
                    personal_info.setAnimation(commonFunctions.animateLeft());
                    contact_info.setAnimation(commonFunctions.animateRight());
                    personal_info.setVisibility(view.GONE);
                    professional_info.setVisibility(view.GONE);
                    contact_info.setVisibility(view.VISIBLE);
                }

                break;

            case R.id.bt_continue_2:



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

                    }
                });

                if (str_mobile.equals("") || str_state.equals("Select State") || str_city.equals("") || str_area.equals("") || str_address.equals("")) {


                    commonFunctions.showAlert(this, "INVALID", "Please fill all the mandatory details");
                    personal_info.setVisibility(view.GONE);
                    professional_info.setVisibility(view.GONE);
                    contact_info.setVisibility(view.VISIBLE);

                } else {
                    header.setText("Professional Info");


                    contact_info.setAnimation(commonFunctions.animateLeft());
                    professional_info.setAnimation(commonFunctions.animateRight());
                    personal_info.setVisibility(view.GONE);
                    contact_info.setVisibility(view.GONE);
                    professional_info.setVisibility(view.VISIBLE);
                }
                break;


            case R.id.bt_save:
//                AddUpdateJobResource async = new AddUpdateJobResource (personal_info.this, "0", "34588A34-E969-4723-84FE-E5409B66A5B7", "", str_first_name, str_last_name, gender_id, age_substring, nationality_id, "IND", "INDIA", state_code, str_state, str_city, str_area, str_address, str_mobile, profession_id, religion_id, "2", months_substring, yrs_substring, "0.00", "0.00", "hssuraksha");
//                async.execute ();
                AddResources addResources = new AddResources();
                addResources.execute();


        }

    }

异步任务

public class getCity extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            commonFunctions.showProgressBar(personal_info.this, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONObject jsonObject = new JSONObject(s.trim());
                JSONObject NewDataSet = jsonObject.getJSONObject("NewDataSet");
                JSONArray tableArray = NewDataSet.getJSONArray("Table");
                for (int i = 0; i < tableArray.length(); i++) {
                    JSONObject table = tableArray.getJSONObject(i);
                    String cityValues = table.getString("City_District_Name");
                    cityArray[i] = cityValues;
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, R.layout.textview_list, cityArray);
                    city.setAdapter(adapter);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
//            commonFunctions.showProgressBar(personal_info.this, false);
        }

        @Override
        protected String doInBackground(String... strings) {
            String response;
            response = HttpRequest.post("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/getCityDistrictForStates").send("State_Name=" + strings[0]).body();
            response = response.replaceAll("<[^>]*>", "").replaceAll("\n", "");
            Log.e("STate city", "" + response);
            return response;
        }
    }

1 个答案:

答案 0 :(得分:0)

这里有两件事要看:

1。)您正在onClick()方法中为微调器注册setOnItemSelectedListener。因此,每次单击都会注册新的setOnItemSelectedListener。尝试在活动中仅为Spinner注册一次setOnItemSelectedListener 2.)精简器的selected属性为true。我的意思是默认是第一个被选中?
同样是什么时候调用onClick()方法?