1

データベースのコンテンツ (特定のテーブルと特定の列) をリストに表示する次のコードがありますが、強制的に閉じるダイアログが表示され、その理由がわかりません。

これは私のコードです:

public class Server8 extends Activity {

DBAdapter db;

    @Override
    public void onCreate (Bundle savedInstanceState) {

       setContentView(R.layout.main);

       db=new DBAdapter(this);
       db.openDataBase();
       Cursor cursor=db.getAllData2();

       startManagingCursor(cursor);

       String[] from=new String[] {db.KEY_ROUTE};     

       int[] to = new int[] { R.id.name_entry};

       ContactListCursorAdapter items = new ContactListCursorAdapter(this, R.layout.list_example_entry, cursor, from, to);
    }

    public class ContactListCursorAdapter extends SimpleCursorAdapter{

        private Context context;

        private int layout;

        public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.context = context;
            this.layout = layout;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            Cursor c = getCursor();

            final LayoutInflater inflater = LayoutInflater.from(context);

            View v = inflater.inflate(layout, parent, false);

            int nameCol = c.getColumnIndex(db.KEY_ROUTE);
            String name = c.getString(nameCol);

            TextView name_text = (TextView) v.findViewById(R.id.name_entry);
            if (name_text != null) {
                name_text.setText(name);
            }

            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor c) {

            int nameCol = c.getColumnIndex(db.KEY_ROUTE);
            String name = c.getString(nameCol);

            TextView name_text = (TextView) v.findViewById(R.id.name_entry);

            if (name_text != null) {
                name_text.setText(name);
            }
        }

私のDBAdapterにはこれがあります:

public static final String TABLE_2= "route";

public static final String KEY_ROWID_2="_id";

public static final String KEY_ROUTE= "route";

public static final String KEY_USER_ID= "user_id";

これは、カーソルを照会する方法です。

public Cursor getAllData2() 
{
    return db.query(TABLE_2, new String[] {KEY_ROUTE},null,null,null,null,null);
}

これは私のlogcatが私に与えるものです:

04-30 09:26:24.323: エラー/AndroidRuntime(2676): 原因: java.lang.IllegalArgumentException: 列 '_id' が存在しません

04-30 09:26:24.323: エラー/AndroidRuntime(2676): android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) で

04-30 09:26:24.323: エラー/AndroidRuntime(2676): android.widget.CursorAdapter.init(CursorAdapter.java:111) で

04-30 09:26:24.323: エラー/AndroidRuntime(2676): android.widget.CursorAdapter で。(CursorAdapter.java:90)

04-30 09:26:24.323: エラー/AndroidRuntime(2676): android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:47) で

04-30 09:26:24.323: エラー/AndroidRuntime(2676): android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:88) で

04-30 09:26:24.323: エラー/AndroidRuntime(2676): com.server.Server8$ContactListCursorAdapter.(Server8.java:103) で

04-30 09:26:24.323: エラー/AndroidRuntime (2676): com.server.Server8.onCreate (Server8.java:91) で


原因: java.lang.IllegalArgumentException: 列 '_id' が存在しません

こんなもの求めてません!!

そして、これは私TABLE_2のように見えるものです:

_id         route                  user_id

1           Sebes-Alba               1             ....only one record

私が間違っていることは何ですか?ありがとう

4

2 に答える 2

2

テーブルに という名前の列があることを確認してください_id。一般的なアプローチは、_id 列を として作成すること_id INTEGER PRIMARY KEY AUTOINCREMENTです。次に、データベースへの各クエリで
常にクエリを実行するようにしてください。_id

参考文献:

列 ID の問題の詳細

列の作成方法

于 2011-04-30T21:57:11.990 に答える
0

ログには_id、クエリのターゲット (テーブル) に列がないことが示されています。

列 '_id' が存在しません

routeテーブルに という名前の列があるかどうかを確認し、_id違いがある場合は を更新する必要がありますpublic static final String KEY_ROWID_2

于 2011-04-30T21:52:12.050 に答える