在我的活动中,我有 editText's (Username, Firstname, Lastname and Email Address)。当用户输入现有用户名时,他可以通过单击搜索按钮进行搜索,用户拥有的数据将出现在 editText 中。
MainActivity.java
btn_Search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String searchableUser = txt_User.getText().toString();
ConsUserRegistration consUserRegistration = db.searchUser(searchableUser);
String searchUser = consUserRegistration.getUser().toString();
String searchFirst = consUserRegistration.getFirstName().toString();
String searchLast = consUserRegistration.getLastName().toString();
String searchEmail = consUserRegistration.getEmail().toString();
txt_User.setText(searchUser);
txt_First.setText(searchFirst);
txt_Last.setText(searchLast);
txt_Email.setText(searchEmail);
}
});
数据库处理程序.java
public ConsUserRegistration searchUser(String username){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Constants.TABLE_USER, new String[] {Constants.KEY_USER, Constants.KEY_FIRST,
Constants.KEY_LAST, Constants.KEY_EMAIL}, Constants.KEY_USER + " =? ",
new String[] { String.valueOf(username) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
ConsUserRegistration search = new ConsUserRegistration (cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3));
return search;
}
但是当用户单击搜索按钮并且没有从用户名 EditText 输入任何字符时,我CursorIndexOutOfBoundsException
在 DatabaseHandler 中遇到错误: ConsUserRegistration search = new ConsUserRegistration (cursor.getString(0), cursor.getString(1) , cursor.getString(2), cursor.getString(3));
此外,由于数据库崩溃,我无法从数据库中检索数据。