如何根据微调器项目选择位置显示值列表?

时间:2012-06-27 07:02:04

标签: android spinner

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

2 个答案:

答案 0 :(得分:2)

我认为问题在于构建数据

结构数据如下

1-使用Bean / Model将所有数据作为一个单元从json中包装

if (loadseq == index) {
                 Dealer dealer  = new Dealer();
                 dealer.setVIN(js.getString("vin");)
                  .......
                   .....

               al.add(dealer);





    public class Dealer{ 
        private String dealer_name1 ;
        private Vehicle ;
        private String vin ;
        private String color ;
        private String parking ;
        private String vindesc ;

         //getter seeter of all


        @Override
            public String toString() {
                return dealer_name1;
            }
        }




        class mySpinnerListener implements Spinner.OnItemSelectedListener {

            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position,
                    long id) {
                Dealer d = (Dealer) parent.getItemAtPosition(position);

                or 

                Dealer d = al.elmentAt(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

答案 1 :(得分:2)

嘿,这对我有用......首先是......说这是国家的微调器

/ <强> * ** * ** * ** * ** * * 国家/地区的微调器 * ** * ** * ** * *** /

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*******************/
}

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