复选框和SQL数据库更新

时间:2013-05-22 03:19:22

标签: java android

您好我有一个地址簿应用程序,我在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     

time they need a db access public DatabaseConnector(Context context){ // create a new DatabaseOpenHelper (its an inner class coded below that extends SQLiteOpenHelper, see its constructor for this constructors parameter semantics) 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){ } } }

1 个答案:

答案 0 :(得分:1)

将布尔值转换为字符串

editContact.put("favourite", favourite.toString());