Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

java - Update City Spinner with notifyDataSetChanged after state is selected

How do I update my city spinner once the user selects a state?

Both fields are populated using a DataCall.class that returns JSON data and parses the info into an array for the spinner.

My code below sets the city adapter to a defualt "Select State" value and once the user gets selects the state it should use notifyDataSetChanged since the default array for the city spinner is updated with the new city names. The errors I am getting are commented in my code below.

public class SearchActivity extends Activity{
       private static final String TAG = "MyApp";
       ArrayAdapter<String> adapter2;
       String city_values[] = new String[]{"Please select a state."};

       @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);       

            adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values);
            adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
            cityspinner.setAdapter(adapter2);

            JSONArray jsonArray;
            try {
                String spinnerContentType = "state";
                String spinnerURL = "getStoreState.php";
                String spinner_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 
                Log.d(TAG, spinner_data);
                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);    
                    Log.d(TAG, styleValue);
                    array_spinner[i] = styleValue;
                }

                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<?> parent,View view, int pos, long id) {
                            int item = zipspinner.getSelectedItemPosition();
                            if(item != 0){

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

                                JSONArray cityArray = null;
                                try {
                                    cityArray = new JSONArray(city_data);
                                } catch (JSONException e) {
                                     e.printStackTrace();
                                }       
                                final String[] city_spinner = new String[cityArray.length()]; 

                                for (int i=0; i<cityArray.length(); i++){                       
                                    String styleValue = null;
                                    try {
                                        styleValue = cityArray.getJSONArray(i).getString(0);
                                        Log.d(TAG, styleValue);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }              
                                    city_spinner[i] = styleValue;                               
                                }

                                city_values = city_spinner; 
                                adapter2.notifyDataSetChanged();

                                String test_string = "NOTIFY UPDATE";
                                Log.d(TAG, test_string);
                            } else {
                               // finish();
                            }
                        }
                        public void onNothingSelected(AdapterView parent) {
                          // Do nothing.
                        }
                   });

                   cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
                            int item = zipspinner.getSelectedItemPosition();
                            if(item != 0){

                            }else{

                            }

                        }

                        public void onNothingSelected(AdapterView parent) {
                          // Do nothing.
                        }
                  });   
          }catch (JSONException e) {
                e.printStackTrace();
          }
     }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, this is how I will suggest,

First of all check that you are getting values in city_values. Then, notify the adapter.... adapter2.notifyDataSetChanged(); And finally cityspinner.setSelection(0);

UPDATE:

I would suggest to trake ArrayList<String> instead of String[]

Thanks...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...