基于另一个微调器选择填充微调器

时间:2016-04-05 17:24:12

标签: android spinner

我有一个带有身体部位列表的微调器,例如。胸部,腿部,背部。我也有每个身体部位的字符串数组,其中包含该身体部位的练习列表,例如。对于胸部我有一个字符串阵列称为胸部运动与选择的运动,如卧推,哑铃按等。 因此,例如,如果选择了胸部,或者如果选择了腿,则我想用胸部锻炼来填充第二个旋转器。任何想法?

 String[] chestExercises = {"Flat Barbell Bench Press", "Incline Barbell Bench Press",
            "Decline Barbell Bench Press", "Flat Dumbbell Press", "incline Dumbbell Press", "Decline Dumbbell Press",
            "Cable Flys", "Dumbbell Flys",};


    public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());


   }


    public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);



    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(record_workout.this,
                    "OnClickListener : " +
              "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem())+
               "\nSpinner 2 : String.valueOf(spinner2.getSelectedItem()),
                    Toast.LENGTH_SHORT).show();
        }

    });
}

1 个答案:

答案 0 :(得分:1)

您需要做的就是检查spinner1输入,并根据该输入,根据与某个arraylist匹配的用户输入设置与spinner2对应的数组适配器:

我为你写了这段代码以获得更好的想法,并在其中添加了一些评论,所以也要检查一下。

public void addListenerOnSpinnerItemSelection() {
    ArrayList<MyObject> Chest = new ArrayList<MyObject>(); // This is actually the list created from the HashMap
    ArrayList<MyObject> Knee = new ArrayList<MyObject>(); // This is actually the list created from the HashMap
    ArrayList<MyObject> Head = new ArrayList<MyObject>(); // This is actually the list created from the HashMap

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}


public void addListenerOnButton() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(record_workout.this,
                          "OnClickListener : " +
                          "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem())+
                          "\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
                           Toast.LENGTH_SHORT).show();

            ArrayAdapter<MyObject> adapter = null;

            if(spinner1.getSelectedItem().equals("Chest"){
                adapter = new ArrayAdapter<ObjectName>(this, android.R.layout.simple_spinner_item, Chest);
            } else if (spinner1.getSelectedItem().equals("Knee"){ //INPUT = KNEE SO SET ADAPTER TO KNEE ARRAY LIST
                adapter = new ArrayAdapter<ObjectName>(this, android.R.layout.simple_spinner_item, Knee);
            } else if (spinner1.getSelectedItem().equals("Head"){
                adapter = new ArrayAdapter<ObjectName>(this, android.R.layout.simple_spinner_item, head);
            }

            if (adapter != null) {
                spinner2.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });
    }
}

我在这里做的是我在你的微调器监听器中检查了第一个微调器中的输入。然后,我检查了它是否匹配&#34; head&#34;,&#34; chest&#34;或&#34; Knee&#34;,如果确实如此,我设置了ArrayAdapter对于Spinner 2到相应的ArrayList

如果这有帮助,请告诉我。