android.database.CursorIndexOutOfBoundsException

发布于 2024-12-06 06:29:07 字数 1884 浏览 0 评论 0原文

我正在运行该软件,但是当我滚动列表视图时,它显示正常,但过了一会儿我收到此错误消息:

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

我的适配器类:

  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);

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

最好的问候, 尼科斯

I am running the software but when i scroll with the listview, its showing ok, but after a while i get this error message:

I am trying to display a cursor to a listview.

My Adapter class:

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

My Constructor

        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 Method

        @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; 
        }
    }

Simple View Holder

    public static class ViewHolder {
        public TextView tvHome;
    }
}

Adapter init

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);

Can anyone help me to solve this problem?I've searched everywhere but i could not find any solution

Best regards,
Nicos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆躺在深渊里 2024-12-13 06:29:07

在实现自己的光标适配器时,您应该覆盖 newViewbindView而不是 getView。我发现这个: 尝试在 SimpleCursorAdapter 中覆盖 getView 会给出 NullPointerException< /a>

you're supposed to override newView and bindView when implementing your own cursor adapter, not getView. i found this: Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio

断肠人 2024-12-13 06:29:07

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

Since you have only single element you have put index as 0.
You try this one c.getString(0). I think it will work

物价感观 2024-12-13 06:29:07
holder.tvHome.setText(c.getString(3)+"("+c.getString(6)+")");
if(position<=c.getCount())         
   c.moveToNext();
return convertView;    
holder.tvHome.setText(c.getString(3)+"("+c.getString(6)+")");
if(position<=c.getCount())         
   c.moveToNext();
return convertView;    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文