强制关闭创建简单的游标适配器

时间:2011-04-30 21:38:30

标签: android database simplecursoradapter

我有以下代码,它应该在列表中显示来自数据库(某个表和某个列)的内容,但是我得到一个强制关闭对话框,我不知道为什么。

这是我的代码:

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:ERROR / AndroidRuntime(2676):引起:java.lang.IllegalArgumentException:列'_id'不存在

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):在android.widget.CursorAdapter.init(CursorAdapter.java:111)

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):在android.widget.CursorAdapter。(CursorAdapter.java:90)

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):在android.widget.ResourceCursorAdapter。(ResourceCursorAdapter.java:47)

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):在android.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:88)

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):at com.server.Server8 $ ContactListCursorAdapter。(Server8.java:103)

04-30 09:26:24.323:ERROR / AndroidRuntime(2676):at com.server.Server8.onCreate(Server8.java:91)


引起:java.lang.IllegalArgumentException:列'_id'不存在

我甚至不想要这样的东西!!

这就是我TABLE_2的样子:

_id         route                  user_id

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

我做错了什么?感谢

2 个答案:

答案 0 :(得分:2)

确保您的表格中有一个名为_id的列。常见的方法是将_id列创建为_id INTEGER PRIMARY KEY AUTOINCREMENT 然后,确保始终在每次查询数据库时查询_id

参考文献:

Details on column ID issues

How to create a column

答案 1 :(得分:0)

您的日志显示查询目标中没有_id列(表格):

  

列'_id'不存在

您应该验证route表中是否有名为_id的列,如果存在差异,则应更新public static final String KEY_ROWID_2