android - 如何根据微调项目选定的位置显示值列表?

标签 android spinner

我有一个微调器,我正在填充它。我有值列表,并且我也得到了微调器的选定位置。现在我想根据微调器项目选定的位置显示值列表。怎么做。有人可以帮我吗?

最佳答案

嘿,这对我有用......第一件事是......说这是国家的旋转器

/************ **国家旋转器*******>******/

ArrayList<NameValuePair> alcountry = new ArrayList<NameValuePair>();
                    try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://blah.com/country.php");
                        httppost.setEntity(new UrlEncodedFormEntity(alcountry));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                        }catch(Exception e){
                            Log.e("log_tag", "Error in http connection"+e.toString());
                       }

                        try{
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                             sb = new StringBuilder();
                             sb.append(reader.readLine() + "\n");

                             String line="0";
                             while ((line = reader.readLine()) != null) {
                                            sb.append(line + "\n");
                              }
                              is.close();
                              Countryresult=sb.toString();
                              }catch(Exception e){
                                    Log.e("log_tag", "Error converting result "+e.toString());
                              }

                              try{
                                    countryarray = new JSONArray(Countryresult);
                                    JSONObject json_data=null;
                                    for(int i=0;i<countryarray.length();i++){
                                           json_data = countryarray.getJSONObject(i);
                                           Countrylist.add(json_data.getString("country_name"));
                                           System.out.println("hq value is " +Countrylist);
                                           Countryadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Countrylist);
                                           country.setAdapter(Countryadapter);
                                           country.setOnItemSelectedListener(this);
                                       }
                                    }
                                    catch(JSONException e1){
                                      Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
                                    } catch (ParseException e1) {
                                        e1.printStackTrace();
                                }


                    /*****************************end of spinner for country*******************/

然后现在在重写的方法中

@Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
            long arg3) {
        // TODO Auto-generated method stub
        state = (Spinner)findViewById(R.id.etstate);
        Statelist = new ArrayList<String>();

    /*****************************spinner for state based on country**************************/

    ArrayList<NameValuePair> alstate = new ArrayList<NameValuePair>();
    alstate.add(new BasicNameValuePair("country_name",country.getSelectedItem().toString()));
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://blah.com/state.php");
        httppost.setEntity(new UrlEncodedFormEntity(alstate));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
       }

        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
             sb = new StringBuilder();
             sb.append(reader.readLine() + "\n");

             String line="0";
             while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
              }
              is.close();
              Stateresult=sb.toString();
              }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
              }

              try{
                    statearray = new JSONArray(Stateresult);
                    JSONObject json_data=null;
                    for(int i=0;i<statearray.length();i++){
                           json_data = statearray.getJSONObject(i);
                           Statelist.add(json_data.getString("state_name"));
                           System.out.println("hq value is " +Statelist);
                           Stateadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Statelist);
                           state.setAdapter(Stateadapter);

                       }
                    }
                    catch(JSONException e1){
                      Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                }

                    state.setOnItemSelectedListener(this);
    /*****************************end of spinner for state*******************/
}

所以这就是你要做的......

关于android - 如何根据微调项目选定的位置显示值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11220962/

相关文章:

android - 在 MediaPlayer 中控制音量

java - 具有多项选择的 Android Spinner

vb.net - 当应用程序搜索数据库时添加旋转轮

reactjs - Ant Design react - 更改所有组件的默认微调器

android - ActivityGroup 子项中的微调器不起作用

android - 为什么我的进度对话框在 new intent start() 时不旋转?

android - 在 Firebase 中获取 child 的名字

java - 读取隐藏的 zip 文件

java - SQLite Android - MODE_WORLD_WRITEABLE 无法解析为变量

Android 4.4.2(api级别= 19)Webview问题