android.database.CursorIndexOutOfBoundsException

时间:2011-09-23 08:51:24

标签: android listview

我正在运行该软件,但是当我使用listview滚动时,它显示正常,但过了一段时间我收到此错误消息:

我正在尝试将光标显示到列表视图。

我的适配器类:

  private class DBAdapter extends SimpleCursorAdapter {
        private LayoutInflater mInflater;
        private Cursor c;
        private Context context;

我的构造函数

        public DBAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.c = c;
            this.context = context;
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

getView方法

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null; 
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.match_item, null);
                holder.tvHome = (TextView)convertView.findViewById(R.id.ll_home);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
                holder.tvHome.setText(c.getString(3)+"("+c.getString(6)+")");
                c.moveToNext();
                return convertView; 
        }
    }

简单查看持有人

    public static class ViewHolder {
        public TextView tvHome;
    }
}

适配器初始化

Cursor cur = dh.getAll();
    startManagingCursor(cur);

    String[] from = new String[] { "home" };
    int[] to = new int[] { R.id.ll_home};

    // Now create an array adapter and set it to display using our row
    DBAdapter matches = new DBAdapter(this,R.layout.match_item, cur, from, to);
    setListAdapter(matches);

任何人都可以帮我解决这个问题吗?我到处搜索但我找不到任何解决方案

祝你好运, 尼科斯

3 个答案:

答案 0 :(得分:0)

holder.tvHome.setText(c.getString(3)+"("+c.getString(6)+")");
if(position<=c.getCount())         
   c.moveToNext();
return convertView;    

答案 1 :(得分:0)

由于您只有单个元素,因此您将index设置为0。 你试试这个c.getString(0)。我认为它会起作用

答案 2 :(得分:0)

在实现自己的游标适配器时,您应该覆盖newViewbindView不是 getView。我发现了这个:Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio