当我运行该程序时,一切看起来都不错,但是当我在搜索栏中输入一个字母时,程序立即崩溃。它主要显示LibraryDbAdapter类和searchBar()
的{{1}}函数的异常在onQueryTextChange(String newText)
课程中。我也在使用SearchViewActivity
。
LibraryDbAdapter.java
ListView
SearchViewActivity.java
package com.example.waheed.library;
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;
/**
* Created by Waheed on 6/30/17.
*/
public class LibraryDbAdapter {
public static final String KEY_ROWID = "rowid";
//public static final String KEY_LIBRARY = "library";
public static final String KEY_BOOKNAME = "bookname";
public static final String KEY_BOOKAUTHOR = "bookauthor";
public static final String KEY_STORENAME = "storename";
public static final String KEY_STOREADDRESS = "storeaddress";
public static final String KEY_STORELAT = "storelat";
public static final String KEY_STORELONG = "storelong";
public static final String KEY_SEARCH = "searchData";
private static final String TAG = "LibraryDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "LibraryData";
private static final String FTS_VIRTUAL_TABLE = "LibraryInfo";
private static final int DATABASE_VERSION = 1;
//Create a FTS3 Virtual Table for fast searches
private static final String DATABASE_CREATE =
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
// KEY_LIBRARY + "," +
KEY_BOOKNAME + "," +
KEY_BOOKAUTHOR + "," +
KEY_STORENAME + "," +
KEY_STOREADDRESS + "," +
KEY_STORELAT + "," +
KEY_STORELONG + "," +
KEY_SEARCH + "," +
" UNIQUE (" + KEY_BOOKNAME + "));";
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) {
Log.w(TAG, DATABASE_CREATE);
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 " + FTS_VIRTUAL_TABLE);
onCreate(db);
}
}
public LibraryDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public LibraryDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createLibrary( String bookname, String bookauthor, String storename, String storeaddress, String storelat, String storelong) {
ContentValues initialValues = new ContentValues();
String searchValue = bookname;
//initialValues.put(KEY_LIBRARY, library);
initialValues.put(KEY_BOOKNAME, bookname);
initialValues.put(KEY_BOOKAUTHOR, bookauthor);
initialValues.put(KEY_STORENAME, storename);
initialValues.put(KEY_STOREADDRESS, storeaddress);
initialValues.put(KEY_STORELAT, storelat);
initialValues.put(KEY_STORELONG, storelong);
initialValues.put(KEY_SEARCH, searchValue);
return mDb.insert(FTS_VIRTUAL_TABLE, null, initialValues);
}
public Cursor searchBook(String inputText) throws SQLException {
Log.w(TAG, inputText);
String query = "SELECT " +
KEY_BOOKNAME +
" from " + FTS_VIRTUAL_TABLE +
" where " + KEY_SEARCH + " MATCH '" + inputText + "';";
Log.w(TAG, query);
Cursor mCursor = mDb.rawQuery(query, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean deleteAllLibrary() {
int doneDelete = 0;
doneDelete = mDb.delete(FTS_VIRTUAL_TABLE, null, null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
}