这里BookList.java是一个微调器类,它维护两个出现,一个是书,另一个是作者,都有自己的列表视图类。通过在OnItemSelected()方法内切换案例,可以访问它们的意图。但我面临的问题是,无法访问第一个列表视图应用的意图,但是可以访问作者意图。如果有人帮我编辑我的代码会很有帮助。这是java文件。
public class BookList extends Activity implements OnItemSelectedListener {
Spinner spinner;
//private static final String[]paths = {"Book", "Author"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booklist);
TextView t =(TextView)findViewById(R.id.t1) ;
spinner = (Spinner)findViewById(R.id.spinner);
//ArrayAdapter<String>adapter = new ArrayAdapter<String>(BookList.this,
//android.R.layout.simple_spinner_item,paths);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.blist_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
final Intent intent;
switch (position) {
case 1:
intent = new Intent(BookList.this, BookListView.class);
startActivity(intent);
break;
case 2:
intent = new Intent(BookList.this, BookListAuthorView.class);
startActivity(intent);
break;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(this, "Please choose a field", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
项目位置从0
开始,而不是从1
开始,因此请使用此
所以0位置会在你第一次打开你的活动时自动触发,所以为spinner创建一个这样的微调器数组
String arr ={"select choice","book","author"};
switch (position) {
case 1:
intent = new Intent(BookList.this, BookListView.class);
startActivity(intent);
break;
case 2:
intent = new Intent(BookList.this, BookListAuthorView.class);
startActivity(intent);
break;
}
加上你不必调用`adapter.notifyDataSetChanged();'