2016-04-05 152 views
1

我有一個微調器與身體部位列表例如。胸部,腿部,背部。我也有每個身體部位的字符串數組,例如身體部位的鍛鍊列表。對於胸部,我有一個字符串陣列稱爲胸部鍛鍊與選擇的鍛鍊,如臥推,啞鈴按等 因此,例如我想填充第二個微調胸部鍛鍊,如果選擇胸部或腿部鍛鍊,如果腿被選中。任何ides?基於另一個微調器選擇填充微調器

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

所有你需要做的是檢查spinner1輸入,並此基礎上,設置對應於spinner2基於用戶輸入匹配一定的ArrayList中的數組適配器:

我寫了這代碼讓你得到一個更好的主意,並添加了一些評論,所以檢查出來。

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

讓我知道這是否有幫助。

+0

感謝您的代碼,但我不能讓它工作。我編輯原來的帖子,現在包括胸部運動的陣列。所以當胸部被選中時,我想讓陣列內容出現 – Ryan159

+0

它不起作用。我明白你想做什麼,但我不明白這部分:ArrayList Chest = new ArrayList (); – Ryan159

+0

這是一個名爲Chest的數組列表,當用戶選擇「胸部」 –

相關問題