0

嗨,我有一个地址簿应用程序,我在 xml 中添加了一个“收藏夹”复选框。现在我尝试将复选框链接到代码中的 sql 数据库连接器类,以便在保存、更新、编辑新联系人时它也可以更新数据库。我遇到了麻烦,因为复选框是一个布尔值,因为用于联系人的所有其他字段都只是字符串,所以当我为复选框使用字符串时,我的应用程序停止运行。下面是数据库连接器类,我已经指出了我添加的代码行。

// 数据库连接器.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     

他们需要数据库访问的时间 公共数据库连接器(上下文上下文){ // 创建一个新的 DatabaseOpenHelper(它是一个在下面编码的内部类,它扩展了 SQLiteOpenHelper,请参阅它的构造函数以了解此构造函数参数语义) databaseOpenHelper = new 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){ } } }

4

1 回答 1

1

将布尔值转换为字符串

editContact.put("favourite", favourite.toString());
于 2013-05-22T05:24:04.480 回答