0

从微调器中选择一个值后,我希望让另一个值列表从所做的选择中填充微调器。例如:当用户单击微调器时,会出现值“主队、主队、主队其他”。用户单击其中之一,然后与该选择相关的玩家然后填充微调器。下面是填充的原始微调器的代码。

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

谢谢。

4

1 回答 1

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成员变量,以便您可以在 this 中的任何位置使用它Activity。您还需要添加implements OnItemSelectedListener到您的类定义

public class ExampleMain extends Activity implements OnItemSelectedListener{

此外,您正在adapter使用看起来相同的值进行多次初始化。只需初始化一次,然后设置你AdaptersSpinners

于 2013-06-12T16:08:01.223 回答