简体   繁体   English

Android:连接到SQLite数据库..光标问题

[英]Android: Connecting to SQLite database.. Problem with Cursor

private class MContactsAdapter extends SimpleCursorAdapter {

    private Context context;

    private DataBaseHelper dbHelper;

    private Cursor currentCursor;

    public 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. 我通过不同的方法创建了这个Adapter类。 Whenever it call super(context, layout, cursor, from, to) method it through exception. 每当它通过异常调用super(context,layout,cursor,from,to)方法时。 because of cursor. 因为光标。 I am filling my cursor with sqlite database table. 我用sqlite数据库表填充我的光标。 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. 如果我给null而不是光标,那么它工作正常:S如果有任何人有任何想法..我会提前评估.. Thanx。

As is in CursorAdapter documentation: CursorAdapter文档中一样:

The Cursor must include a column named "_id" or this class will not work. Cursor必须包含名为“_id”的列,否则此类将不起作用。

How to: 如何:

  • Add alias in database query to id 将数据库查询中的别名添加到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. 或者,如果您不需要按ID区分行,请输入查询伪_id。

Example: 例:

SELECT 1 _id, name, address FROM user
  • Or extend other Adapter. 或者扩展其他适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM