android.database.CursorIndexOutOfBoundsException
我正在运行该软件,但是当我滚动列表视图时,它显示正常,但过了一会儿我收到此错误消息:
我正在尝试将光标显示到列表视图。
我的适配器类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在实现自己的光标适配器时,您应该覆盖
newView
和bindView
,而不是getView
。我发现这个: 尝试在 SimpleCursorAdapter 中覆盖 getView 会给出 NullPointerException< /a>you're supposed to override
newView
andbindView
when implementing your own cursor adapter, notgetView
. i found this: Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio由于您只有单个元素,因此您将索引设置为 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