我在db上填写了 listview 。我想在 listview 中搜索。 我想在listview上的列表后搜索名称,姓氏和公司字段。 此代码返回 NullPointerException 。
myhomeclass:
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.asrehber);
dbHelper = new DBAdapter(this);
dbHelper.open();
Cursor cursor = dbHelper.getAllPerson();
String[] from = new String[] { DBAdapter.KEY_NAME,
DBAdapter.KEY_SURNAME, DBAdapter.KEY_COMPANY };
int[] to = new int[] { R.id.btnUserNameListItem,
R.id.btnUserSurnameNameListItem };
dataAdapter = new SimpleCursorAdapter(this,
R.layout.asrehber_single_list_item, cursor, from, to);
ListView listView = (ListView) findViewById(R.id.listViewMain);
listView.setAdapter(dataAdapter);
dbHelper.close();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent viewCon = new Intent(ASRehber.this, PersonProfile.class);
viewCon.putExtra(ROW_ID, id);
startActivity(viewCon);
finish();
}
});
// Custom Search/////////////
edtxtSearch = (EditText) findViewById(R.id.edtxtSearch);
edtxtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
ASRehber.this.dataAdapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
mydataadapter:
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 DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_SURNAME = "surname";
public static final String KEY_COMPANY = "company";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHONE = "phone";
private static final String TAG = "DBAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "ASRehber";
private static final String DATABASE_TABLE = "Person";
private static final int DATABASE_VERSION = 1;
public static final String[] ALL_KEYS = new String[] { KEY_ROWID, KEY_NAME,
KEY_SURNAME, KEY_COMPANY, KEY_EMAIL, KEY_PHONE };
private final Context mCtx;
private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer PRIMARY KEY autoincrement," + KEY_NAME + ","
+ KEY_SURNAME + "," + KEY_COMPANY + "," + KEY_EMAIL + ","
+ KEY_PHONE + ");";
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 " + DATABASE_TABLE);
onCreate(db);
}
}
public DBAdapter(Context ctx) {
this.mCtx = ctx;
}
public DBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public Cursor getAllPerson() {
String where = null;
Cursor c = mDb.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getPerson(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = mDb.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public long insertPerson(String name, String surname, String company,
String email, String phone) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_SURNAME, surname);
initialValues.put(KEY_COMPANY, company);
initialValues.put(KEY_EMAIL, email);
initialValues.put(KEY_PHONE, phone);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deletePerson(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return mDb.delete(DATABASE_TABLE, where, null) != 0;
}
public boolean updateRow(long rowId, String name, String surname,
String company, String email, String phone) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, name);
newValues.put(KEY_SURNAME, surname);
newValues.put(KEY_COMPANY, company);
newValues.put(KEY_EMAIL, email);
newValues.put(KEY_PHONE, phone);
// Insert it into the database.
return mDb.update(DATABASE_TABLE, newValues, where, null) != 0;
}
}
答案 0 :(得分:0)
嗯,这很简单。 dbHelper.getAllPerson();
如果找不到任何匹配数据,则有可能返回null。你只需要处理它。
答案 1 :(得分:0)
1)不要关闭数据库,直到你不再需要它的任何一个游标。 SimpleCursorAdapter(以及几乎所有使用游标的东西)并不会将所有数据保存在内存中,而是在需要时会查询它的新行(例如,当用户滚动列表或尝试过滤时列表)。而是使用它在onDestory()
Activity方法中关闭数据库。
2)当您关闭某些内容(光标,数据库,流 - 任何内容)时,请在finally {}
块中执行此操作。
3)你真的应该考虑在ContentProvider中使用存储数据并使用Loaders来访问它 - 这种方法使数据库访问变得更加简单,并避免长时间SQL查询拖延主线程。