0

我在扩展 BaseAdapter 的类中使用了 Cursor,

class NewDefinedAdapter extends BaseAdapter {

    private Cursor cursorTitle;
    private TextView title;
    private LayoutInflater mInflater;
    private LinearLayout layout;

    public NewDefinedAdapter(Context text, Cursor cursor) {
        cursorTitle = cursor;
        mInflater = LayoutInflater.from(text);
    }

    public int getCount() {
        return cursorTitle.getCount();
    }

    public Object getItem(int arg0) {
        return arg0;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        layout = (LinearLayout) mInflater.inflate(R.layout.title_list, null);
        title = (TextView) layout.findViewById(R.id.title);
        title.setText(cursorTitle.getString(0));
        return layout;
    }
}

我这样称呼这个类:

cursor.moveToFirst();
view.setAdapter(new NewDefinedAdapter(this, cursor));

cursor.getCount() = 10,但最终,我得到了 10 个相同的结果。添加 cursor.MoveToNext() 后,程序运行异常:

layout = (LinearLayout) mInflater.inflate(R.layout.title_list, null);
title = (TextView) layout.findViewById(R.id.title);
title.setText(cursorTitle.getString(0));
cursorTitle.moveToNext();
return layout;

异常信息:

04-07 14:27:28.565: D/AndroidRuntime(26155): Shutting down VM
04-07 14:27:28.565: W/dalvikvm(26155): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-07 14:27:28.575: E/AndroidRuntime(26155): FATAL EXCEPTION: main
04-07 14:27:28.575: E/AndroidRuntime(26155): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 10
04-07 14:27:28.575: E/AndroidRuntime(26155):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)

那么,我应该在光标中获取所有值,谢谢。

4

1 回答 1

1

使用moveToPosition()

//...
layout = (LinearLayout) mInflater.inflate(R.layout.title_list, null);
title = (TextView) layout.findViewById(R.id.title);
cursorTitle.moveToPosition(position);
title.setText(cursorTitle.getString(0));
//...
于 2012-04-07T14:36:58.143 回答