2015-08-14 19 views
-1

我在我所注册的ListView控件的上下文菜单一个ListView片段:非空的ListView给错误,大小为0

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中?

+0

显示CourseArrayAdapter的代码。您可能没有正确添加代码到getItem()或getCount()。 –

+0

@TheOriginalAndroid好吧,我更新它 –

+0

面临完全相同的问题。你解决了吗? –

回答

0

我怀疑你是不是在正确的代码修改适配器:

register.removeCourse(((CourseArrayAdapter) getListAdapter()).getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position).getId()); 

OR

register.editCourse(... 

这是困难的,因为你的代码确实为我提出这个完整的代码修复不适合我的编码风格,当然我不太了解你的代码。不过,我正在提供示例代码以从适配器中删除项目。基本上,如果您想要添加,删除或编辑List<Course>集合中的数据,则需要通知适配器。 示例代码:

adapter = getListAdapter(); 

adapter.remove(adapter.getItem(position)); 
adapter.notifyDataSetChanged(); 

我希望这是不够清楚,你得到一个良好的开端。

相关问题