こんにちは、アドレス帳アプリを持っています。xml で「お気に入り」チェックボックスを追加しました。新しい連絡先が保存、更新、編集されたときにデータベースも更新できるように、チェックボックスをコード内の sql データベース コネクタ クラスにリンクしようとしています。チェックボックスはブール値であり、連絡先に使用される他のすべてのフィールドは単なる文字列であるため、問題が発生しているため、チェックボックスに文字列を使用するとアプリの実行が停止します。以下はデータベース コネクタ クラスです。追加したコード行を示しています。
// DatabaseConnector.java// Provides easy connection and creation of UserContacts database. public class DatabaseConnector{ // database name private static final String DATABASE_NAME = "UserContacts"; private SQLiteDatabase database; // database object private DatabaseOpenHelper databaseOpenHelper; // database helper // public constructor for DatabaseConnector - other activity classes create one every
データベースへのアクセスが必要な時間 public DatabaseConnector(コンテキスト コンテキスト){ // 新しい DatabaseOpenHelper を作成します (SQLiteOpenHelper を拡張する以下にコード化された内部クラスです。このコンストラクターのパラメーター セマンティクスについては、そのコンストラクターを参照してください) databaseOpenHelper = 新しい DatabaseOpenHelper(context, DATABASE_NAME, null, 1); }
// open the database connection public void open() throws SQLException{ //any error in the method will cause the specified (imported) exception // create OR open a database for reading/writing database = databaseOpenHelper.getWritableDatabase(); //inherited from SQLiteOpenHelper which DatabaseOpenHelper extends } // close the database connection public void close(){ if (database != null) database.close(); //inherited from SQLiteOpenHelper which DatabaseOpenHelper extends } // insert (Add), update (Edit) and delete do not require any display so no cursor returned (in either case there is a return to the "intenting" Activity as soon as Save/Delete(after confirm dialog) button pressed // inserts a new contact in the database public void insertContact(String name, String email, String phone, String state, String city/*,Boolean favourite*/){ ContentValues newContact = new ContentValues(); // required as parameter type by SQLiteDatabase.insert(...) - key/value data structure newContact.put("name", name); newContact.put("email", email); newContact.put("phone", phone); newContact.put("street", state); newContact.put("city", city); newContact.put("favourite",favourite); //code i added open(); // open()coded above database.insert("contacts", null, newContact); // parameters: table, not used here (has to do with inserting empty records), ContentValues object close(); // close() coded above } // updates an existing contact in the database public void updateContact(long id, String name, String email, String phone, String state, String city/*, Boolean favourite*/){ ContentValues editContact = new ContentValues(); // required as parameter type by SQLiteDatabase.update(...) - key/value data structure editContact.put("name", name); editContact.put("email", email); editContact.put("phone", phone); editContact.put("street", state); editContact.put("city", city); editContact.put("favourite", favourite); // code i added; open(); database.update("contacts", editContact, "_id=" + id, null); // parameters: table, ContentValues object, where clause, where arguments (not used here but allows compound where conditions) close(); } // delete the contact specified by the given String name public void deleteContact(long id){ open(); // parameters: table, where clause without where, ... database.delete("contacts", "_id=" + id, null); // parameters: close(); } // viewing all or just one contact require data to be returned (via a cursor) to the call point for display // return a Cursor with all contact information in the database public Cursor getAllContacts(){ // parameters: table, columns in a String array, ... other SQL SELECT statement stuff return database.query("contacts", new String[] {"_id", "name"}, null, null, null, null, "name"); } // get a Cursor containing all information about the contact specified by the given id public Cursor getOneContact(long id){ // parameters: table, null = return all columns, where clause without where, ... other SQL SELECT statement stuff return database.query("contacts", null, "_id=" + id, null, null, null, null); } private class DatabaseOpenHelper extends SQLiteOpenHelper{ public DatabaseOpenHelper(Context context, String name, CursorFactory factory, int version){ // if a db schema version higher than the one on the device is supplied onUpgrade will run onUpgrade to upgrade the schema appropriately (which we must code of course) // so new versions of the App using new schema versions of the db will be able to update the db schema and will function correctly (without data loss) super(context, name, factory, version); // parameters: from constructor call (context, DATABASE_NAME, null, 1) // null = use the default cursor factory, 1 = version 1 of the database wrt structure (schema) NOT data } // creates the contacts table when the database is created @Override //required public void onCreate(SQLiteDatabase db){ //only executes if db does not already exist // query to create a new table named contacts // the naming of the column _id is important since this specifies the record id used elsewhere when accessing the table String createQuery = "CREATE TABLE contacts" + "(_id integer primary key autoincrement," + "name TEXT, email TEXT, phone TEXT," + "street TEXT, city TEXT);"; db.execSQL(createQuery); } @Override //required public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ } } }