选择状态后使用 notificationDataSetChanged 更新 City Spinner

发布于 2024-12-04 06:52:23 字数 5150 浏览 1 评论 0 原文

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

这两个字段均使用返回 JSON 数据并解析的 DataCall.class 进行填充将信息放入微调器的数组中。

我下面的代码将城市适配器设置为默认的“选择状态”值,一旦用户选择了状态,就应该使用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();
          }
     }
}

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

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

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

发布评论

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

评论(3

維他命╮ 2024-12-11 06:52:23

好吧,这就是我的建议,

首先检查您是否在 city_values 中获取值。
然后,通知适配器....adapter2.notifyDataSetChanged();
最后 cityspinner.setSelection(0);

UPDATE:

我建议使用 ArrayList;而不是 String[]

谢谢...

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...

玩物 2024-12-11 06:52:23
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();
        }
    }
}

现在尝试

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

now try

双手揣兜 2024-12-11 06:52:23

在分配 new JSONArray() 的位置声明 city_array 变量。反正你是暂时用的。只需重新设置适配器即可。

Declare the city_array variable at the place where you assign a new JSONArray(). Anyways you are using it temporarily. Just set the adapter again.

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