场景是 - 用户从下拉列表中选择学期(sem1,sem2 ...等)值,从另一个中选择其分支(cs,it ...),并根据所选的两个值,listview包含指定的学期和分支得到填充,并根据该科目我想显示该科目的教学大纲..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.syllabus);
String[] semarray = new String[] { "Sem 1", "Sem 2", "Sem 3" };
String[] brancharray = new String[] { "CS", "EC", "EE" };
semester = (Spinner) findViewById(R.id.sems);
branch = (Spinner) findViewById(R.id.branch);
semarrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, semarray);
semester.setAdapter(semarrayAdapter);
brancharrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, brancharray);
branch.setAdapter(brancharrayAdapter);
semester.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
item1 = semester.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
branch.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
item2 = branch.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
if(item1 == "Sem 1" && item2 == "CS"){
Toast.makeText(this, "hi me hu",Toast.LENGTH_SHORT).show();
};
}
如何实施???请帮助
我是android的新手
答案 0 :(得分:0)
试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.syllabus);
String[] semarray = new String[] { "Sem 1", "Sem 2", "Sem 3" };
String[] brancharray = new String[] { "CS", "EC", "EE" };
semester = (Spinner) findViewById(R.id.sems);
branch = (Spinner) findViewById(R.id.branch);
listView = (ListView) findViewById(R.id.listview);
semarrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, semarray);
semester.setAdapter(semarrayAdapter);
brancharrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, brancharray);
branch.setAdapter(brancharrayAdapter);
semester.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
item1 = semester.getSelectedItem().toString();
if(item1 != null && item2 != null){
String[] listArray = new String[]{item1 + " " + item2};
listViewAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listArray);
listView.setAdapter(listViewAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
branch.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
item2 = branch.getSelectedItem().toString();
if(item1 != null && item2 != null){
String[] listArray = new String[]{item1 + " " + item2};
listViewAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listArray);
listView.setAdapter(listViewAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
if(item1 == "Sem 1" && item2 == "CS"){
Toast.makeText(this, "hi me hu",Toast.LENGTH_SHORT).show();
};
}