如何修复[方法put(String,Boolean)在ContentValues类型中不适用于参数(布尔值,布尔值)]

时间:2014-05-16 22:31:48

标签: java android

我正在尝试学习如何开发Android应用程序,并尝试重新编写 Notepadv3Solution {http://developer.android.com/training/notepad/index.html}为了我自己的目的修改它。

在方法createTask(nr底部)中,我收到错误The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean)。我为数据库添加了一个布尔字段,并将其添加到createTask方法中。如何才能使布尔字段起作用?

package com.superiorxc.taskcentral;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TasksDbAdapter {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_BODY = "body";
    public static final Boolean KEY_COMPLETE = true;

    private static final String TAG = "TasksDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;    

    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table tbl_tasks (_id integer primary key autoincrement, "
        + "title text not null, body text, complete boolean not null);";

    private static final String DATABASE_NAME = "db_taskcentral";
    private static final String DATABASE_TABLE = "tbl_tasks";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;    

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS tbl_tasks");
            onCreate(db);
        }
    }    

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public TasksDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the db_taskcentral database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public TasksDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }   

    /**
     * Create a new task using the title and body provided. If the task is
     * successfully created return the new rowId for that note, otherwise return
     * a -1 to indicate failure.
     * 
     * @param title the title of the task
     * @param body the body of the task
     * @return rowId or -1 if failed
     */
    public long createTask(String title, String body, Boolean complete) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_BODY, body);
        initialValues.put(KEY_COMPLETE,complete);


        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }    

}

3 个答案:

答案 0 :(得分:1)

KEY_COMPLETE更改为

public static final String KEY_COMPLETE = "complete";

您希望它引用您在数据库中命名列的内容。

您可以将 ContentValues 视为Map<String,Object>,其中键是数据库列的名称,对象是您要放入该列的行的对象。

答案 1 :(得分:-1)

如果您问“如何将布尔值转换为字符串”,那么最简单的方法就是:

String.valueOf(Boolean)

,如下面的代码所示:

    Boolean b = false;
    String aString = String.valueOf(b);
    System.out.println(aString);

答案 2 :(得分:-1)

一种解决方案是改变

public static final Boolean KEY_COMPLETE = true;

public static final String KEY_COMPLETE = "true";