开发者

Android: Connecting to SQLite database.. Problem with Cursor

private class MContactsAdapter extends SimpleCursorAdapter {

    private Context context;

    private DataBaseHelper dbHelper;

    private Cursor currentCursor;

    pu开发者_如何转开发blic MContactsAdapter(Context context, int layout, Cursor c,

    String[] from, int[] to, DataBaseHelper dbHelper) {

        super(context, layout, null, from, to);

        this.currentCursor = c;

        this.context = context;

        this.dbHelper = dbHelper;
    }

    public View getView(int pos, View inView, ViewGroup parent) {
        View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.main, null);
        }

        this.currentCursor.moveToPosition(pos);

        TextView cBox = (TextView) v.findViewById(R.id.txtDisplayName);

        cBox.setText(this.currentCursor
                .getString(this.currentCursor
                        .getColumnIndex("lat")));

        TextView txtTitle = (TextView) v.findViewById(R.id.txtName);
        txtTitle.setText(this.currentCursor.getString(this.currentCursor
                .getColumnIndex("lng")));

        TextView txtaddress = (TextView) v.findViewById(R.id.txtPhone);
        txtaddress.setText(this.currentCursor.getString(this.currentCursor
                .getColumnIndex("address")));

        return (v);
    }

}

I created this Adapter class by different methods. Whenever it call super(context, layout, cursor, from, to) method it through exception. because of cursor. I am filling my cursor with sqlite database table. I dont know why its not acception filled cursor. If i give null instead of cursor, then it works fine :S If anyone have any idea.. I will appriciate.. Thanx in advance.


As is in CursorAdapter documentation:

The Cursor must include a column named "_id" or this class will not work.

How to:

  • Add alias in database query to id

Example:

SELECT id _id, name, address FROM user
  • Or if you don't need to distinct rows by id, put in query fake _id.

Example:

SELECT 1 _id, name, address FROM user
  • Or extend other Adapter.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜