2017-01-28 12 views
1

私はアンドロイドの新機能です。以下のコードでは、エンド・ユーザーが見たいセクションを選択し、リスト・ビューを表示します。私はスピナーをアクティブにすることはできません、私は別のセクションを選択していますが、それは1つだけのリストビューを示しています。アンドロイドスピナーをアクティブにするにはどうすればいいですか?

public class TeacherClasses extends AppCompatActivity implements AdapterView.OnItemClickListener,View.OnClickListener{ 
public String[] Section1 = {"Bonilla, Abbie", "Hernando, Roland Joseph", "Ko, Kritofer", "Manaig, Kathleen", 
     "Olalia, Jerome","Rosario, Kyle", "Sevilla, Karen", "Tancioco, Eron", "Villena, Mark" }; 
public String[] Section2 = {"Chavez, Stephanie", "Espana, Bren Alfred", "Faro, Ede", "Gonzales, Venice", 
     "Magora, Joshua James","Roman, Jairah", "Ramirez, Stephanie", "Tiboli, Jamalul", "Torrazo, Nicole" }; 
public String[] Section3 = {"Arbonida, Caye Anne", "De Guzman, Patricia", "Escandor, Jennifer", "Marzan, Rann", 
     "Menorca, Paula","Payofelin, Marlo", "Pimentel, Iris Coleen", "Queen, Elizabeth", "Unggoy, Monkey" }; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_teacher_classes); 
    Button btn_Sections = (Button) findViewById(R.id.btn_Sections); 
    btn_Sections.setOnClickListener(this); 
    CreateList(); 
} 
private void CreateList() { 

    Spinner spn_Sections = (Spinner) findViewById(R.id.spn_Sections); 
    List<String> Sections = new ArrayList<String>(); 
    Sections.add("Section 1"); 
    Sections.add("Section 2"); 
    Sections.add("Section 3"); 

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Sections); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spn_Sections.setAdapter(dataAdapter); 


    ListAdapter ClassAdapter = null; 
    String s = spn_Sections.getSelectedItem().toString(); 
    if (s.equals("Section 1")) { 
     ClassAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Section1); 
     Toast.makeText(this, "Section 1", Toast.LENGTH_SHORT).show(); 
    } else if (s.equals("Section 2")) { 
     ClassAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Section2); 
     Toast.makeText(this, "Section 2", Toast.LENGTH_SHORT).show(); 
    } else if (s.equals("Section 3")) { 
     ClassAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Section3); 
     Toast.makeText(this, "Section 3", Toast.LENGTH_SHORT).show(); 
    } 
    ListView MyClasses = (ListView)findViewById(R.id.list_ClassList); 
    MyClasses.setAdapter(ClassAdapter); 
    MyClasses.setOnItemClickListener(this); 


} 
+1

あなたのコードのJava規則に従ってください。 –

+0

spinnerItemClickListenerにアダプタを設定 –

答えて

0

代わりにこの方法を試してみてください。

private void CreateList() { 

    Spinner spn_Sections = (Spinner) findViewById(R.id.spn_Sections); 
    List<String> Sections = new ArrayList<String>(); 
    Sections.add("Section 1"); 
    Sections.add("Section 2"); 
    Sections.add("Section 3"); 

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Sections); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spn_Sections.setAdapter(dataAdapter); 


    spn_Sections.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

      switch (position) { 
       case 0: 
        ClassAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Section1); 
        Toast.makeText(getApplicationContext(), "Section 1", Toast.LENGTH_SHORT).show(); 
        break; 
       case 1: 
        ClassAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Section2); 
        Toast.makeText(getApplicationContext(), "Section 2", Toast.LENGTH_SHORT).show(); 
        break; 
       case 2: 
        ClassAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Section3); 
        Toast.makeText(getApplicationContext(), "Section 3", Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

      // sometimes you need nothing here 
     } 
    }); 

    ListView MyClasses = (ListView) findViewById(R.id.list_ClassList); 
    MyClasses.setAdapter(ClassAdapter); 
    MyClasses.setOnItemClickListener(this); 

} 

をあなたはまた、世界的に宣言しspinner

ためsetOnItemSelectedListenerリスナーを設定する必要があります。

ListAdapter ClassAdapter = null; 
+0

これは私にいくつかのエラーを与えます –

+0

エラーは何ですか? – rafsanahmad007

+0

これは私にいくつかのエラーを教えてくれる 1.クラスアダプタ - 「変数クラスアダプタは内部クラスからアクセスされ、最終的に宣言する必要があります。 2.(this、android.R.layout) 3. Toast - 「メソッドを解決できません」と表示されます。makeText(anonymous.widget.AdapterView.OnItemSelectedListener、java。ありがとう@ rafsanahmad007 –

関連する問題