【问题标题】:populating spinner based on another spinner selection根据另一个微调器选择填充微调器
【发布时间】:2023-03-11 21:14:01
【问题描述】:

我有一个带有身体部位列表的微调器,例如。胸部,腿部,背部。我还有每个身体部位的字符串数组,以及该身体部位的练习列表,例如。对于胸部,我有一个称为胸部锻炼的弦乐阵列,其中包括卧推、哑铃推举等一系列练习。 因此,例如,如果选择了胸部,我想用胸部运动填充第二个微调器,如果选择了腿部,我想用腿部运动填充第二个微调器。有什么好办法吗?

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

    });
}

【问题讨论】:

    标签: android spinner


    【解决方案1】:

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

    我编写这段代码是为了让你有一个更好的想法,并在其中添加了一些 cmets,所以也请检查一下。

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

    我在这里所做的是检查了微调器侦听器中第一个微调器中的输入。然后,我检查它是否匹配“头部”、“胸部”或“膝盖”,如果匹配,我将 Spinner 2 的 ArrayAdapter 设置为对应的 ArrayList

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

    【讨论】:

    • 感谢您的代码,但我无法正常工作。我编辑了原来的帖子,它现在包括胸部锻炼的数组。 so when chest is selected i would like to have that arrays contents appear
    • 它不起作用。我了解您要做什么,但我不明白这部分:ArrayList Chest = new ArrayList();
    • 这是一个名为 Chest 的数组列表,其中填充了您在用户选择 chest 时在微调器中想要的值
    • 它说“无法解析符号'MyObject'”。这是我应该如何创建我的数组吗? (对不起,我对 android 非常陌生)` String[] legExercises = {“深蹲”,“腿部推举”,“弓步”,“腿部伸展”,“腿部卷曲”,“坐姿提踵”,“站立提踵”} ; final ArrayAdapter legAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, legExercises);`
    • @user3091082 用字符串替换单词MyObject
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多