Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在android中选择单行时出错_Android - Fatal编程技术网

我在android中选择单行时出错

我在android中选择单行时出错,android,Android,我在启动程序时遇到问题 我想知道如何使用TableBook类中的arab_book函数 这是课桌 package info.androidhive.slidingmenu; public class Book { private int id; private String title; private String author; public Book() {} public Book(String title, String auther)

我在启动程序时遇到问题

我想知道如何使用TableBook类中的arab_book函数 这是课桌

package info.androidhive.slidingmenu;

public class Book {

    private int id;
    private String title;
    private String author;

    public Book() {}

    public Book(String title, String auther) {
        this.title = title;
        this.author = auther;
    }

    // ---- setter
    public void setId(int id){
           this.id = id;
    }

    public void setTitle(String title){
           this.title = title;
    }

    public void setAuthor(String author){
           this.author = author;
    }


    // --- getter ---

    public int getId(){
           return id;
    }

    public String getTitle(){
           return title;
    }

    public String getAuthor(){
           return author;
    }

    public String toString(){
           return "Book  >> id:"+id+" | title:"+title+" | author:"+author;
    }
}
这是书桌

package info.androidhive.slidingmenu;

import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BookTable extends SQLiteOpenHelper {


    private static final int DB_VERSION = 1;

    private static final String DB_NAME = "Book";

    //Book Table name
    private static final String TABLE_BOOK = "books";

    //Book Table Columns name
    private static final String KEY_ID = "id";
    private static final String KEY_TITLE = "title";
    private static final String KEY_AUTHER = "author";

    private static final String[] COLUMNS = {KEY_ID, KEY_TITLE, KEY_AUTHER};


    public BookTable(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        // SQL statement to create book table
        String CREATE_BOOK_TABLE = "CREATE TABLE books ( " +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "title TEXT, "+
                "author TEXT )";
        // create books table
        db.execSQL(CREATE_BOOK_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS books");

        // create fresh books table
        this.onCreate(db);
    }

    /*
     * --------------------------------------------------------------------------------
     */

    public void addBook(Book book) {
        Log.d("addBook", book.toString());

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_TITLE, book.getTitle());
        values.put(KEY_AUTHER, book.getAuthor());

        db.insert(TABLE_BOOK, null, values);
        db.close();
    }
    public Book arab_book(String id){
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor c = db.query(TABLE_BOOK, COLUMNS, KEY_ID, new String[] {id}, null, null, null);
        if( c != null )
            c.moveToFirst();

        Book book = new Book();
        book.setId(Integer.parseInt(c.getString(0)));
        book.setTitle(c.getString(1));
        book.setAuthor(c.getString(2));

        return book;
    }

    public Book getBook(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor c = db.query(TABLE_BOOK, COLUMNS, KEY_ID, new String[] {String.valueOf(id)}, null, null, null, null);
        if( c != null )
            c.moveToFirst();

        Book book = new Book();
        book.setId(c.getInt(c.getColumnIndex(KEY_ID)));
        book.setTitle((c.getString(c.getColumnIndex(KEY_TITLE))));
        book.setAuthor(c.getString(c.getColumnIndex(KEY_AUTHER)));
        //Log.d("GetBook("+id+")", book.toString());

        return book;
    }

    // Get All Books
    public Book[] getAllBooks() {
        Book list[] = null;

        // 1. build the query
        String query = "SELECT  * FROM " + TABLE_BOOK;

        // 2. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        System.out.println("Count of data in Book table >>"+cursor.getCount());

        list = new Book[cursor.getCount()];

        // 3. go over each row, build book and add it to list
        Book book = null;
        int index = 0;
        if (cursor.moveToFirst()) {
            do {
                book = new Book();
                book.setId(Integer.parseInt(cursor.getString(0)));
                book.setTitle(cursor.getString(1));
                book.setAuthor(cursor.getString(2));

                // Add book to books
                list[index]= book;
                System.out.println("Book "+index+": ");
                index++;

            } while (cursor.moveToNext());
        }
/*
        //Log.d("getAllBooks()", books.toString());
        for(int i=0; i<list.length; i++){
              System.out.println("Book "+i+": "+list[i].getTitle());
        }*/

        // return books
        return list;
    }

 // Updating single book
    public int updateBook(Book book) {

        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // 2. create ContentValues to add key "column"/value
        ContentValues values = new ContentValues();
        values.put("title", book.getTitle()); // get title
        values.put("author", book.getAuthor()); // get author

        // 3. updating row
        int i = db.update(TABLE_BOOK, //table
                values, // column/value
                KEY_ID+" = ?", // selections
                new String[] { String.valueOf(book.getId()) }); //selection args

        // 4. close
        db.close();

        return i;

    }

 // Deleting single book
    public void deleteBook(Book book) {

        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // 2. delete
        db.delete(TABLE_BOOK,
                KEY_ID+" = ?",
                new String[] { String.valueOf(book.getId()) });

        // 3. close
        db.close();

        Log.d("deleteBook", book.toString());

    }
}
当我在主活动中使用此代码时,我有一个错误

 bt = new BookTable(getActivity().getApplicationContext());

 Book bs = bt.arab_book("4");

您对查询方法的调用是错误的。 尝试阅读该方法的文档。 第三个参数是selection,第四个参数是SelectionAgs

SelectionArray中的所有元素都将替换选择参数中相应的问号

尝试将getBook和arab_book中的代码更改为:

但总的来说,您应该尝试阅读Android中使用数据库的基础知识。 考虑使用A和CURSOR适配器,从中读取。 您可以在中找到更多信息


注意:这可能不是唯一的问题。请发布logcat输出以提供更多详细信息。

您是否尝试过使用logcat?通过ADB这里有一个关于它的半体面的教程:这里有一些关于编程记录的信息:您是否在MainActivity中使用getActivity?我在MainActivity bt=new BookTablegetActivity.getApplicationContext中使用此代码;书籍bs=bt.arab_书籍4;
 bt = new BookTable(getActivity().getApplicationContext());

 Book bs = bt.arab_book("4");
Cursor c = db.query(
    TABLE_BOOK, //table
    COLUMNS, //projection (or columns)
    KEY_ID + "=?", //my where-clause
    new String[]{String.valueOf(id)}, //arguments inside the where-claus
    null, //groupBy
    null, //having
    null, //orderBy
    null); //limit