コースワーク用に (残念ながら) Android アプリケーションを開発しています。予定のカスタム ListView を表示するには、SimpleCursorAdapter を使用する必要があります。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 public class MainActivity extends 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);
//...
}
}
}