为什么SQLite会抱怨在包含时没有找到_id列

时间:2012-10-04 11:16:51

标签: android sqlite listview simplecursoradapter

我正在为我的课程开发一个Android应用程序(不幸的是)。我需要使用SimpleCursorAdapter来显示约会的自定义ListView。 SimpleCursorAdapter的构造函数将游标作为参数,此游标需要选择需要在listView中显示的所有列。

我的问题是,每次运行应用程序时,都会抱怨:“列'_id'不存在'但是有一个名为id的列!我哪里错了?

以下是相关代码:

public class AppointmentsDB {

    public static final String DATABASE_NAME = "appointment_db";
    public static final String DATABASE_TABLE = "appointment_table";
    private static final int DATABASE_VERSION = 1;

    public static final String KEY_ID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_PRIORITY = "priority";
    public static final String KEY_DATE_TIME = "date_time";
    public static final String KEY_DURATION = "duration";
    public static final String KEY_ALARM_TIME = "alarm_time";

    private DatabaseHelper appointmentsDBHelper;
    private SQLiteDatabase appointmentsDB;
    private final Context context;

    public AppointmentsDB(Context context) {
        this.context = context;
        appointmentsDBHelper = new DatabaseHelper(context);
    }

    public SQLiteDatabase openReadableDatabase()
    {
        return appointmentsDBHelper.getReadableDatabase();
    }

    public SQLiteDatabase openWritableDatabase()
    {
        return appointmentsDBHelper.getWritableDatabase();
    }

    public long addAppointment(Appointment appointment)
    {
        //
    }

    public void close()
    {
        //
    }

    static class DatabaseHelper extends SQLiteOpenHelper {

        Context context;

        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

            this.context = context;

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            StringBuilder query = new StringBuilder();
            query.append("CREATE TABLE "+DATABASE_TABLE+" ");
            query.append("(");
            query.append(KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, ");
            query.append(KEY_TITLE+" TEXT NOT NULL, ");
            query.append(KEY_DESCRIPTION+" TEXT, ");
            query.append(KEY_PRIORITY+" INTEGER, ");
            query.append(KEY_DATE_TIME+" INTEGER, ");
            query.append(KEY_DURATION+" INTEGER, ");
            query.append(KEY_ALARM_TIME+" INTEGER");
            query.append(");");

            ContentValues test = new ContentValues();

            db.execSQL(query.toString());
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
            onCreate(db);
        }

    }
}

MainActivity.java 公共类MainActivity扩展了ListActivity {

private Cursor cursor;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AppointmentsDB dbHelper = new AppointmentsDB(this);

    // Execute query that selects all appointments.
    // Store cursor from that query in local variable 'cursor'.
    // Pass 'cursor' to SimpleCursorAdapter.
    // SimpleCursorAdapter uses 'cursor' to display all appointments in
    // listview.

    StringBuilder selectQuery = new StringBuilder();
    selectQuery.append("SELECT "+AppointmentsDB.KEY_ID+" ,");
    selectQuery.append(AppointmentsDB.KEY_TITLE + ", ");
    selectQuery.append(AppointmentsDB.KEY_DESCRIPTION + ", ");
    selectQuery.append(AppointmentsDB.KEY_PRIORITY + ", ");
    selectQuery.append(AppointmentsDB.KEY_DATE_TIME + ", ");
    selectQuery.append(AppointmentsDB.KEY_DURATION + ", ");
    selectQuery.append(AppointmentsDB.KEY_ALARM_TIME + " FROM "
            + AppointmentsDB.DATABASE_TABLE /*+ " "*/);
    selectQuery.append("ORDER BY " + AppointmentsDB.KEY_DATE_TIME
            + " ASC");

    cursor = dbHelper.openReadableDatabase().rawQuery(
            selectQuery.toString(), null);

    String[] columnNames = new String[]{""};
    int[] ids = new int[]{};

    AppointmentsCursorAdapter adapter = new AppointmentsCursorAdapter(this,R.layout.row,cursor,columnNames,ids);
    this.setListAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

// This class sets our customised layout for the ListView

class AppointmentsCursorAdapter extends SimpleCursorAdapter
{

    private Context context;
    private int layout;
    private int[] colours;

    public AppointmentsCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) 
    {
        super(context, layout, c, from, to);

        this.context = context;
        this.layout = layout;
        //...
    }


    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(layout, parent, false);

        TextView titleText = (TextView) view.findViewById(R.id.titleText);
        TextView priorityView = (TextView) view.findViewById(R.id.priorityView);
        TextView dateText = (TextView) view.findViewById(R.id.dateText);
        TextView monthText = (TextView) view.findViewById(R.id.monthText);
        TextView timeText = (TextView) view.findViewById(R.id.timeText);

        //...

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        super.bindView(view, context, cursor);


        TextView titleText = (TextView) view.findViewById(R.id.titleText);
        TextView priorityView = (TextView) view.findViewById(R.id.priorityView);
        TextView dateText = (TextView) view.findViewById(R.id.dateText);
        TextView monthText = (TextView) view.findViewById(R.id.monthText);
        TextView timeText = (TextView) view.findViewById(R.id.timeText);

        //...

    }
}

}

2 个答案:

答案 0 :(得分:2)

将列重命名为id并将select语句更改为:“SELECT”+ AppointmentsDB.KEY_ID +“AS _id”,

答案 1 :(得分:1)

试试这个:

String[] columnNames = new String[]{AppointmentsDB.KEY_ID};
int[] ids = new int[]{R.id.the_id_of_the_edittext_textview_or_wahtever_you_use_in_row_layout};

另外,除非你想做一些奇特的事情,否则你可以简单地使用像这样的SimpleCursorAdapter:

ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columnNames, ids, CursorAdapter.NO_SELECTION);
setListAdapter(adapter);

我还建议使用ListFragment而不是ListActivity,以便在需要时可以在其他活动或情境(平板电脑......)中重复使用该列表。