选择状态微调器后填充城市微调器

时间:2011-08-27 07:23:29

标签: java android spinner android-arrayadapter

这是我的问题:

如何让两个微调器“状态”和“城市”,但在用户首先选择状态之前,城市将为空。

我正在使用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();
            }





      }

}

1 个答案:

答案 0 :(得分:1)

首先设置所有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.
    }

});