将微调器选定值传递给另一个值

时间:2013-06-12 15:47:46

标签: android json spinner android-spinner

一旦从微调器中选择了一个值,我希望从所做的选择中填充微调器的另一个值列表。例如:当用户点击微调器时,它们会显示值" Home Team,Home Subs,Home Other"过来。用户单击其中一个,然后与该选择关联的玩家填充微调器。下面是填充的原始微调器的代码。

public class ExampleMain extends Activity {

        JSONArray jsonArray = null;
        JSONArray str_login = null;
        public String kode;
        public String Team_Name;
        public String Home_team;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.main);  

            JSONParser jParser = new JSONParser();
            String link_url = "http://10.0.2.2/NPD/P_name.php";
            JSONObject json = jParser.FunctionParser(link_url);
            Spinner d11 = (Spinner)findViewById(R.id.doubles11);
            Spinner d12 = (Spinner)findViewById(R.id.doubles12);
            Spinner d21 = (Spinner)findViewById(R.id.doubles21);
            Spinner d22 = (Spinner)findViewById(R.id.doubles22);
            Spinner d31 = (Spinner)findViewById(R.id.doubles31);
            Spinner d32 = (Spinner)findViewById(R.id.doubles32);


                try {
                        jsonArray = json.getJSONArray("team");
                        final String[] items = new String[jsonArray.length()];
                        for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);           
                        items[i]=jsonObject.getString("P_name");
                        }

           ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, 
                   android.R.layout.simple_spinner_item, items);      
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           d11.setAdapter(adapter);


           adapter = new ArrayAdapter<String>(
                    this,android.R.layout.simple_spinner_item, items);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           d12.setAdapter(adapter);

           adapter = new ArrayAdapter<String>(
                   this,android.R.layout.simple_spinner_item, items);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           d21.setAdapter(adapter);

           adapter = new ArrayAdapter<String>(
                   this,android.R.layout.simple_spinner_item, items);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           d22.setAdapter(adapter);

           adapter = new ArrayAdapter<String>(
                   this,android.R.layout.simple_spinner_item, items);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           d31.setAdapter(adapter);

           adapter = new ArrayAdapter<String>(
                   this,android.R.layout.simple_spinner_item, items);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           d32.setAdapter(adapter);




                } catch (JSONException e) {
                        e.printStackTrace();
                }

感谢。

1 个答案:

答案 0 :(得分:0)

您需要使用OnItemSelectedListener

d11.setOnItemSelectedListener(this);

    @Override
    public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
    {
         item = spinner.getItemAtPosition(position).toString();
    }

     public void onNothingSelected(AdapterView parent) {
    // Do nothing.
    }

现在您将拥有自己的价值,您可以随时随地使用它来填充其他Spinners。在此示例中将item设为成员变量,以便您可以在此Activity中的任何位置使用它。您还需要将implements OnItemSelectedListener添加到班级定义

public class ExampleMain extends Activity implements OnItemSelectedListener{

此外,您使用相同的值初始化adapter多次。只需初始化一次,然后在Adapters

上设置Spinners即可