我是android编程的新手,下面的代码将使最终用户选择他想要查看的部分并显示列表视图。我无法激活微调器,它只显示一个列表视图,即使我选择不同的部分。
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);
}
答案 0 :(得分: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);
}
您必须为setOnItemSelectedListener
spinner
听众
同时全球宣布;
ListAdapter ClassAdapter = null;