小编典典

选择状态后,使用notifyDataSetChanged更新City Spinner

json

用户选择州后,如何更新我的城市微调器?

这两个字段均使用填充DataCall.class,该返回JSON数据并将信息解析为微调框的数组。

下面的代码将城市adapter设置为默认的 “选择州”
值,一旦用户选择了该州,则应使用notifyDataSetChanged,因为城市微调框的默认数组已使用新的城市名称进行了更新。我在下面的代码中评论了我遇到的错误。

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();
          }
     }
}

阅读 347

收藏
2020-07-27

共1个答案

小编典典

public class SearchActivity extends Activity {
    ArrayAdapter<String> adapter2;
    String city_values[];

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

        String city_values[] = new String[]{"Please select a state."};
        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;
        final JSONArray cityArray;

        try {
            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;
            }

            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 AdapterView.OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    int item = zipspinner.getSelectedItemPosition();
                    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);               //The final local variable cityArray cannot be assigned, since it is defined in an enclosing type

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


                    for (int i = 0; i < cityArray.length(); i++) {
                        String styleValue = cityArray.getJSONArray(i).getString(0); //Unhandled exception type JSONException                
                        city_spinner[i] = styleValue;
                    }

                    city_values = city_spinner; //Unhandled exception type JSONException
                    adapter2.notifyDataSetChanged();
                }

                public void onNothingSelected(AdapterView parent {
                    // Do nothing.
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

现在尝试

2020-07-27