Android ArrayList outOfBoundsException数据库错误
嘿,当我在应用程序上运行测试时,我开始从数据库中删除数据库值。一旦我这样做了,我就开始犯这个错误 原因:java.lang.IndexOutOfBoundsException:索引:0,大小:0 它适用于这行代码。 PasswordResults PasswordResults=password.get0;我对此感到困惑,因为在我删除数据库中的某些数据之前,get0总是会在我的recyclerView上获取所选行Android ArrayList outOfBoundsException数据库错误,android,sqlite,arraylist,Android,Sqlite,Arraylist,嘿,当我在应用程序上运行测试时,我开始从数据库中删除数据库值。一旦我这样做了,我就开始犯这个错误 原因:java.lang.IndexOutOfBoundsException:索引:0,大小:0 它适用于这行代码。 PasswordResults PasswordResults=password.get0;我对此感到困惑,因为在我删除数据库中的某些数据之前,get0总是会在我的recyclerView上获取所选行 public void addPassword(String n, String
public void addPassword(String n, String w, String u, String p, String d) {
// get database.
SQLiteDatabase db = getWritableDatabase();
try {
// add scores.
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, n);
values.put(COLUMN_WEBSITE, w);
values.put(COLUMN_USERNAME, u);
values.put(COLUMN_PASSWORD, p);
values.put(COLUMN_DESC, d);
// insert will handle null values. closing.
db.insert(TABLE_NAME, "", values);
db.close();
} catch (Exception e) {
e.getLocalizedMessage();
}
}
public void removePassword(String id){
// get database.
SQLiteDatabase db = getWritableDatabase();
// delete database then close or print error.
try {
db.delete(TABLE_NAME, "_id="+id, null);
db.close();
} catch(Exception e) {
e.getLocalizedMessage();
}
}
public void updatePassword(String id, String n, String w, String u, String p, String d) {
// get database.
SQLiteDatabase db = getWritableDatabase();
//update database then close or print error.
try {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, id);
values.put(COLUMN_NAME, n);
values.put(COLUMN_WEBSITE, w);
values.put(COLUMN_USERNAME, u);
values.put(COLUMN_PASSWORD, p);
values.put(COLUMN_DESC, d);
db.update(TABLE_NAME, values, "_id="+id, null);
db.close();
} catch (Exception e) {
e.getLocalizedMessage();
}
}
public ArrayList<PasswordResults> getPasswords() {
// get database, password array, and select statement.
passwords = new ArrayList<>();
String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME;
SQLiteDatabase db = getWritableDatabase();
// link to database
Cursor cursor = db.rawQuery(refQuery, null);
if(cursor.moveToFirst()) {
do {
PasswordResults passwordResults = new PasswordResults();
passwordResults.entryName = cursor.getString(0);
passwordResults.website = cursor.getString(1);
passwordResults.username = cursor.getString(2);
passwordResults.password = cursor.getString(3);
passwordResults.description = cursor.getString(4);
passwords.add(passwordResults);
} while(cursor.moveToNext());
}
db.close();
return passwords;
}
public ArrayList<PasswordResults> getSelectedPassword(String id) {
// get database, password array, and select statement.
passwords = new ArrayList<>();
String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME+" WHERE "+COLUMN_ID+" = "+id;
SQLiteDatabase db = getWritableDatabase();
// link to database
Cursor cursor = db.rawQuery(refQuery, null);
if(cursor.moveToFirst()) {
do {
PasswordResults passwordResults = new PasswordResults();
passwordResults.id = Integer.parseInt(id);
passwordResults.entryName = cursor.getString(0);
passwordResults.website = cursor.getString(1);
passwordResults.username = cursor.getString(2);
passwordResults.password = cursor.getString(3);
passwordResults.description = cursor.getString(4);
passwords.add(passwordResults);
} while(cursor.moveToNext());
}
db.close();
return passwords;
}
这是我的密码管理器的代码,我在那里得到了错误信息。
我正在传递一个checker整数和一个字符串,该字符串包含用户单击的行的selectedID。我尝试过使用PasswordResults PasswordResults=password.getInteger.parseIntid,认为这样做可行,但实际上不行
// method to load correct screen based on user input.
public void loadScreen(int c, String id){
// checking if it is a previous entry.
if(c == 1) {
// Since there already is a password set button to "Show Password"
password_btn.setText("Show");
// getting selected passwords data.
password = db.getSelectedPassword(id);
// GETTING ERROR HERE
PasswordResults passwordResults = password.get(0);
idSelected = id;
entryName_et.setText(passwordResults.entryName);
website_et.setText(passwordResults.website);
username_et.setText(passwordResults.username);
realPassword = passwordResults.password;
// dont show real password yet.
if(showPW == 1)
password_et.setText(realPassword);
else
password_et.setText("***********");
desc_et.setText(passwordResults.description);
// make delete button visible since user could want to delete entry.
delete_btn.setVisibility(View.VISIBLE);
} else {
// since user is creating a new entry dont show delete button.
delete_btn.setVisibility(View.GONE);
}
}
这是我的数据库类的代码,它处理数据库中信息的检索
public void addPassword(String n, String w, String u, String p, String d) {
// get database.
SQLiteDatabase db = getWritableDatabase();
try {
// add scores.
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, n);
values.put(COLUMN_WEBSITE, w);
values.put(COLUMN_USERNAME, u);
values.put(COLUMN_PASSWORD, p);
values.put(COLUMN_DESC, d);
// insert will handle null values. closing.
db.insert(TABLE_NAME, "", values);
db.close();
} catch (Exception e) {
e.getLocalizedMessage();
}
}
public void removePassword(String id){
// get database.
SQLiteDatabase db = getWritableDatabase();
// delete database then close or print error.
try {
db.delete(TABLE_NAME, "_id="+id, null);
db.close();
} catch(Exception e) {
e.getLocalizedMessage();
}
}
public void updatePassword(String id, String n, String w, String u, String p, String d) {
// get database.
SQLiteDatabase db = getWritableDatabase();
//update database then close or print error.
try {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, id);
values.put(COLUMN_NAME, n);
values.put(COLUMN_WEBSITE, w);
values.put(COLUMN_USERNAME, u);
values.put(COLUMN_PASSWORD, p);
values.put(COLUMN_DESC, d);
db.update(TABLE_NAME, values, "_id="+id, null);
db.close();
} catch (Exception e) {
e.getLocalizedMessage();
}
}
public ArrayList<PasswordResults> getPasswords() {
// get database, password array, and select statement.
passwords = new ArrayList<>();
String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME;
SQLiteDatabase db = getWritableDatabase();
// link to database
Cursor cursor = db.rawQuery(refQuery, null);
if(cursor.moveToFirst()) {
do {
PasswordResults passwordResults = new PasswordResults();
passwordResults.entryName = cursor.getString(0);
passwordResults.website = cursor.getString(1);
passwordResults.username = cursor.getString(2);
passwordResults.password = cursor.getString(3);
passwordResults.description = cursor.getString(4);
passwords.add(passwordResults);
} while(cursor.moveToNext());
}
db.close();
return passwords;
}
public ArrayList<PasswordResults> getSelectedPassword(String id) {
// get database, password array, and select statement.
passwords = new ArrayList<>();
String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME+" WHERE "+COLUMN_ID+" = "+id;
SQLiteDatabase db = getWritableDatabase();
// link to database
Cursor cursor = db.rawQuery(refQuery, null);
if(cursor.moveToFirst()) {
do {
PasswordResults passwordResults = new PasswordResults();
passwordResults.id = Integer.parseInt(id);
passwordResults.entryName = cursor.getString(0);
passwordResults.website = cursor.getString(1);
passwordResults.username = cursor.getString(2);
passwordResults.password = cursor.getString(3);
passwordResults.description = cursor.getString(4);
passwords.add(passwordResults);
} while(cursor.moveToNext());
}
db.close();
return passwords;
}
这可能是一个很容易的修复方法,就像我在这里介绍的一半内容一样,但提前感谢您的帮助。如果您的id是字符串,您必须将查询中的+id更改为+''+id+'',因此它将被视为字符串
String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME+" WHERE "+COLUMN_ID+" = "+ '"' + id + '"';
getSelectedPassword未选择任何行的赔率。不会输入do…while构造,即cursor.moveToFirst返回false,因此返回的结果是没有元素的空ArrayList,因此索引0超出范围 检查ArrayList的大小,仅当该大小大于0时才尝试获取密码。可能是烤面包片或其他东西来表示错误
显然,您需要确定为什么没有找到正在传递的id,这将是根本原因。请从日志中添加堆栈跟踪,以缩小数组为空的位置。请发布logcatlogcat请。。。。