选择州微调器后填充城市微调器

发布于 2024-12-01 22:58:40 字数 5954 浏览 1 评论 0原文

这是我的问题:

如何拥有两个旋转器“州”和“城市”,但在用户首先选择州之前,城市将为空。

我正在使用 json 数据动态构建我的微调器,您将在下面的代码中看到,一旦状态微调器值为 != 0,那么我将使用状态微调器的项目值并为我的城市执行另一个数据库调用。

我唯一的错误是当我创建新的 ArrayAdapter 来保存城市数据时显示。我讨厌发布我的活动的所有代码,但不确定我的问题出在哪里。

public class SearchActivity extends Activity{
    private static final String TAG = "MyApp";
      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_layout);

           final Spinner zipspinner = (Spinner) findViewById(R.id.zipspinner);
           final Spinner cityspinner = (Spinner) findViewById(R.id.cityspinner);        

            JSONArray jsonArray;
            final JSONArray cityArray;

            try {
//GET STATE VALUES FROM DATACALL (DATABASE)
                String spinnerContentType = "state";
                String spinnerURL = "getStoreState.php";
                String spinner_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 

                jsonArray = new JSONArray(spinner_data);

                final String[] array_spinner = new String[jsonArray.length()]; 


                for (int i=0; i<jsonArray.length(); i++)
                {



                    String styleValue = jsonArray.getJSONArray(i).getString(0); 

                    array_spinner[i] = styleValue;

                }
    //ADD STATE VALUES TO SPINNER           
                ArrayAdapter<String> adapter = 
                    new ArrayAdapter<String> (this, 
                            android.R.layout.simple_spinner_item,array_spinner);

                adapter.setDropDownViewResource(R.layout.state_spinner_layout);
                zipspinner.setAdapter(adapter);

                zipspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {


                        int item = zipspinner.getSelectedItemPosition();

            //IF ITEM IN STATE IS SELECTED NOW GET CITIES FROM DATABALL         
                        if(item != 0){



                            try {
                                String item_value = array_spinner[item];
                                String spinnerContentType = "city";
                                String spinnerURL = "getStoreCity.php?state=" + item_value;
                                String city_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 

                                cityArray = new JSONArray(city_data);

                                final String[] city_spinner = new String[cityArray.length()]; 


                                for (int i=0; i<cityArray.length(); i++)
                                {                       
                                    String styleValue = cityArray.getJSONArray(i).getString(0);                 
                                    city_spinner[i] = styleValue;                               
                                }
                    //THIS IS WHERE MY ISSUE IS TRYING TO ADD THE CITIES TO THEIR SPNNER            
                                ArrayAdapter<String> adapter2 = 
                                    new ArrayAdapter<String> (this, 
                                            android.R.layout.simple_spinner_item,city_spinner);

                                adapter2.setDropDownViewResource(R.layout.city_spinner_layout);

                                cityspinner.setAdapter(adapter2);

                                cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                                        int item = cityspinner.getSelectedItemPosition();


                                        if(item != 0){
                                            String item_value = array_spinner[item];
                                            String nameContentType = "name";
                                            String shopURL = "getStoreList.php?city=" + item_value;

                                            String name_data =  DataCall.getJSON(shopURL,nameContentType);

                                            Bundle bundle = new Bundle();
                                            bundle.putString("shopData", name_data);
                                            Log.v(TAG,name_data);

                                              /** Intent myIntent = new Intent(SearchActivity.this, ShowRestaurant.class);
                                               myIntent.putExtras(bundle);
                                               startActivityForResult(myIntent, 0); */
                                            }
                                        else {
                                           // finish();
                                        }



                                    }

                                    public void onNothingSelected(AdapterView<?> arg0) {
                                    }

                                });


                            }catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }



                            }
                        else {
                           // finish();
                        }



                    }

                    public void onNothingSelected(AdapterView<?> arg0) {
                    }

                });


            }catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }





      }

}

This is my question:

How do I have two spinners "State" and "City" but the city will be empty until the user selects a state first.

I am building my spinners dynamically using json data and you will see in my code below that once the state spinner value is != 0 then I use the item value of the state spinner and do another database call for my cities.

My only error is showing when I create my new ArrayAdapter to hold to the city data. I hate to post all of my code for my activity but not sure where my issue is.

public class SearchActivity extends Activity{
    private static final String TAG = "MyApp";
      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_layout);

           final Spinner zipspinner = (Spinner) findViewById(R.id.zipspinner);
           final Spinner cityspinner = (Spinner) findViewById(R.id.cityspinner);        

            JSONArray jsonArray;
            final JSONArray cityArray;

            try {
//GET STATE VALUES FROM DATACALL (DATABASE)
                String spinnerContentType = "state";
                String spinnerURL = "getStoreState.php";
                String spinner_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 

                jsonArray = new JSONArray(spinner_data);

                final String[] array_spinner = new String[jsonArray.length()]; 


                for (int i=0; i<jsonArray.length(); i++)
                {



                    String styleValue = jsonArray.getJSONArray(i).getString(0); 

                    array_spinner[i] = styleValue;

                }
    //ADD STATE VALUES TO SPINNER           
                ArrayAdapter<String> adapter = 
                    new ArrayAdapter<String> (this, 
                            android.R.layout.simple_spinner_item,array_spinner);

                adapter.setDropDownViewResource(R.layout.state_spinner_layout);
                zipspinner.setAdapter(adapter);

                zipspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {


                        int item = zipspinner.getSelectedItemPosition();

            //IF ITEM IN STATE IS SELECTED NOW GET CITIES FROM DATABALL         
                        if(item != 0){



                            try {
                                String item_value = array_spinner[item];
                                String spinnerContentType = "city";
                                String spinnerURL = "getStoreCity.php?state=" + item_value;
                                String city_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 

                                cityArray = new JSONArray(city_data);

                                final String[] city_spinner = new String[cityArray.length()]; 


                                for (int i=0; i<cityArray.length(); i++)
                                {                       
                                    String styleValue = cityArray.getJSONArray(i).getString(0);                 
                                    city_spinner[i] = styleValue;                               
                                }
                    //THIS IS WHERE MY ISSUE IS TRYING TO ADD THE CITIES TO THEIR SPNNER            
                                ArrayAdapter<String> adapter2 = 
                                    new ArrayAdapter<String> (this, 
                                            android.R.layout.simple_spinner_item,city_spinner);

                                adapter2.setDropDownViewResource(R.layout.city_spinner_layout);

                                cityspinner.setAdapter(adapter2);

                                cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                                        int item = cityspinner.getSelectedItemPosition();


                                        if(item != 0){
                                            String item_value = array_spinner[item];
                                            String nameContentType = "name";
                                            String shopURL = "getStoreList.php?city=" + item_value;

                                            String name_data =  DataCall.getJSON(shopURL,nameContentType);

                                            Bundle bundle = new Bundle();
                                            bundle.putString("shopData", name_data);
                                            Log.v(TAG,name_data);

                                              /** Intent myIntent = new Intent(SearchActivity.this, ShowRestaurant.class);
                                               myIntent.putExtras(bundle);
                                               startActivityForResult(myIntent, 0); */
                                            }
                                        else {
                                           // finish();
                                        }



                                    }

                                    public void onNothingSelected(AdapterView<?> arg0) {
                                    }

                                });


                            }catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }



                            }
                        else {
                           // finish();
                        }



                    }

                    public void onNothingSelected(AdapterView<?> arg0) {
                    }

                });


            }catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }





      }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2024-12-08 22:58:40

首先设置所有 AdapterString 数组,然后在获取城市数据时调用 adapter.notifyDatasetChanged() 。像这样:

String city_values[] = new String[]{"Please select a state."};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_spinner);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(adapter2);

zipspinner 实现一个 OnItemSelectedListener

zipspinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
    public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
        String value = state_values[pos];
        // now get your city list against value.           
        city_values = yourWayOfGettingData(value);
        adapter2.notifyDatasetChanged();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }

});

Set all your Adapters and String arrays first and then just call adapter.notifyDatasetChanged() while you got the data for city. something like this:

String city_values[] = new String[]{"Please select a state."};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_spinner);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(adapter2);

for the zipspinner implement a OnItemSelectedListener.

zipspinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
    public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
        String value = state_values[pos];
        // now get your city list against value.           
        city_values = yourWayOfGettingData(value);
        adapter2.notifyDatasetChanged();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文