0

コースワーク用に (残念ながら) 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);

        //...

    }
}

}

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);

また、必要に応じて他のアクティビティや状況(タブレット...)でリストを再利用できるように、ListActivityの代わりにListFragmentを使用することをお勧めします。

于 2012-10-04T11:31:50.760 に答える