简体   繁体   English

如何修复[ContentValues类型的put(String,Boolean)方法不适用于自变量(Boolean,Boolean)]

[英]How to fix [The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean)]

I'm trying to learn how to develop an Android App and am trying to re-write Notepadv3Solution {http://developer.android.com/training/notepad/index.html} from modifying it for my own purposes. 我正在尝试学习如何开发Android应用程序,并试图出于自己的目的修改Notepadv3Solution {http://developer.android.com/training/notepad/index.html}进行重写。

In the method createTask (nr the bottom), I get the error The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean) . 在方法createTask (底部为nr)中,出现错误The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean) I've added a boolean field for the database, adding it also to the createTask method. 我为数据库添加了一个布尔字段,并将其也添加到createTask方法中。 How can I get it the boolean field to work? 我怎样才能使布尔字段起作用?

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);
    }    

}

Change KEY_COMPLETE to KEY_COMPLETE更改为

public static final String KEY_COMPLETE = "complete";

You want it to reference what you named your column in the database. 您希望它引用您在数据库中命名的列。

You can think of ContentValues as a Map<String,Object> where the key is the name of the database column and the object is what you want to put into the row for that column. 您可以将ContentValues视为Map<String,Object> ,其中键是数据库列的名称,而对象是要放入该列的行中的对象。

If you are asking "How to convert a Boolean to a String" then the easiest way is this: 如果您询问“如何将布尔值转换为字符串”,那么最简单的方法是:

String.valueOf(Boolean)

as in the following code: 如以下代码所示:

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

One solution would be to change 一种解决方案是改变

public static final Boolean KEY_COMPLETE = true;

to

public static final String KEY_COMPLETE = "true";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型 GUI 中的方法 countTrue(boolean[]) 不适用于 arguments (boolean, boolean, boolean, boolean, boolean) - The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolean, boolean, boolean, boolean) 方法put(String,ArrayList <Integer> ),类型为TreeMap <String,ArrayList<Integer> &gt;不适用于参数(字符串,布尔值) - The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean) Intent类型的方法putExtra(String,boolean)不适用于参数(String,CarouselDataItem) - The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, CarouselDataItem) Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值) - The method parseInt(String) in the type Integer is not applicable for the arguments (boolean) ArrayList 类型中的 toArray(T[]) 方法<Boolean>不适用于参数 (boolean[]) - The method toArray(T[]) in the type ArrayList<Boolean> is not applicable for the arguments (boolean[]) 将textView转换为Boolean以避免:Intent类型中的方法putExtra(String,boolean)不适用于参数 - Converting textView to Boolean to avoid: The method putExtra(String, boolean) in the type Intent is not applicable for the arguments 如何修复 String 类型不适用于我的方法中的参数 boolean? - How do I fix a String type isn't applicable for argument boolean in my method? 如何修复字符串中的布尔方法 - How to fix boolean method in string JspWriter类型的方法print(boolean)不适用于参数(void) - The method print(boolean) in the type JspWriter is not applicable for the arguments (void) PrintStream 类型中的方法 println(boolean) 不适用于参数 (void) - The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM