0

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

我的问题是每次我运行应用程序时,它都会抱怨:'column '_id' does not exist' 但是有一个名为 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);

        //...

    }
}

}

4

2 回答 2

2

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

于 2012-10-04T11:59:35.853 回答
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,以便您可以在需要时在其他活动或情况(平板电脑......)中重用该列表。

于 2012-10-04T11:31:50.760 回答