簡體   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);
    }

}

我通過不同的方法創建了這個Adapter類。 每當它通過異常調用super(context,layout,cursor,from,to)方法時。 因為光標。 我用sqlite數據庫表填充我的光標。 我不知道為什么它不接受填充光標。 如果我給null而不是光標,那么它工作正常:S如果有任何人有任何想法..我會提前評估.. Thanx。

CursorAdapter文檔中一樣:

Cursor必須包含名為“_id”的列,否則此類將不起作用。

如何:

  • 將數據庫查詢中的別名添加到id

例:

SELECT id _id, name, address FROM user
  • 或者,如果您不需要按ID區分行,請輸入查詢偽_id。

例:

SELECT 1 _id, name, address FROM user
  • 或者擴展其他適配器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM