2013-05-22 68 views
0

嗨我有一个地址簿应用程序,我在xml中添加了一个“最喜欢的”复选框。现在我试图将复选框链接到代码中的sql数据库连接器类,这样当新联系人被保存,更新,编辑时,它也可以更新数据库。我有麻烦,因为复选框是一个布尔值,因为用于联系人的所有其他字段都只是字符串,所以当我使用字符串作为复选框时,我的应用程序停止运行。下面是数据库连接器类,我已经指出了我添加的代码行。复选框和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  

时间,他们需要一个DB访问 公共DatabaseConnector(上下文的背景下){// 创建一个新的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){ } } }

回答

1

转换布尔串

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