我是 Android 编程新手,下面的代码将使最终用户选择他想要查看的部分并显示 ListView 。我无法使微调器处于 Activity 状态,即使我选择不同的部分,它也只显示一个 ListView 。
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);
}
试试这个方法:
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;
我是一名优秀的程序员,十分优秀!