我有一个ListView片段,我在其中注册了ListView以获取上下文菜单:
CourseArrayAdapter adapter = new CourseArrayAdapter(getActivity(), register.getCourseListByGrade(grade));
setListAdapter(adapter);
getListView().setLongClickable(true);
registerForContextMenu(getListView());
我确信我的适配器包含非空列表,因为ListView中填充了我的自定义视图。
上下文菜单相关方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_course_context, menu);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(((CourseArrayAdapter)getListAdapter()).getItem(info.position).getCourseName());
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_remove:
register.removeCourse(((CourseArrayAdapter) getListAdapter()).getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position).getId());
return true;
case R.id.action_edit:
register.editCourse(((CourseArrayAdapter) getListAdapter()).getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position).getId());
return true;
}
return super.onContextItemSelected(item);
}
在getListAdapter().getItem(info.position)
中调用onCreatedContextMenu
无误地运行,并显示正确的菜单标题。但是,单击选项时会显示以下错误:
08-14 16:02:46.964 1923-1923/com.andhruv.schoolapp E/MessageQueue-JNI﹕ java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
at com.andhruv.schoolapp.CourseListFragment.onContextItemSelected(CourseListFragment.java:62)
at android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:1912)
CourseListFragment.java:62是
行register.editCourse(((CourseArrayAdapter) getListAdapter()).getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position).getId());
CourseArrayAdapter:
class CourseArrayAdapter extends ArrayAdapter<Course> {
private Gpa gpaCalculator;
public CourseArrayAdapter(Context context, List<Course> values) {
super(context, 0,values);
gpaCalculator = new Gpa();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_course,parent,false);
TextView textViewCourseName = (TextView)rowView.findViewById(R.id.textViewCourseName);
TextView textViewGpa = (TextView)rowView.findViewById(R.id.textViewGpa);
TextView textViewGrade = (TextView)rowView.findViewById(R.id.textViewGrade);
TextView textViewType = (TextView)rowView.findViewById(R.id.textViewType);
TextView textViewCategory = (TextView)rowView.findViewById(R.id.textViewCategory);
textViewCourseName.setText(getItem(position).getCourseName());
textViewGpa.setText(String.valueOf(gpaCalculator.calcCourseVal(getItem(position).getLetterGrade(),getItem(position).getCourseLevel(),true)));
textViewType.setText(String.valueOf(getItem(position).getCourseLevel()));
textViewCategory.setText(String.valueOf(getItem(position).getCategory()).replace('_',' '));
textViewGrade.setText(String.valueOf(getItem(position).getLetterGrade()));
textViewCourseName.setTag(getItem(position).getId());
return rowView;
}
}
为什么调用((CourseArrayAdapter)getListAdapter()).getItem(info.position)
在onCreateContextMenu中工作但在onContextItemSelected中不起作用?