2013-05-22 67 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());