ArrayList outOfBoundsException数据库错误

时间:2018-11-26 06:09:37

标签: android sqlite arraylist

嘿,当我在应用程序上运行测试时,我开始从数据库中删除数据库值。一旦我做完,我就开始出现这个错误 Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

用于此行代码。 在我删除数据库中的某些数据之前,PasswordResults passwordResults = password.get(0);一直令人困惑,.get(0)总是会在recyclerView上获得选定的行。

这是我的PasswordManager的代码,我在这里遇到错误。 我传递了一个检查器整数和一个字符串,该字符串具有用户单击的行的selectedID。我曾尝试使用PasswordResults passwordResults = password.get(Integer.parseInt(id))认为可行,但不可行。

// 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);
        }
    }

这是我的Database类的代码,它处理数据库中信息的检索。

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;
}

这是我的PasswordResults类的代码。

public class PasswordResults {
    public int id;
    public String entryName;
    public String website;
    public String username;
    public String password;
    public String description;
}

可以很容易地解决,就像我在这里所做的事情的一半一样,但要感谢您的提前帮助。

2 个答案:

答案 0 :(得分:0)

如果您的id是字符串,则必须在查询中将+id更改为+ '"' + id + '"',因此它将被视为字符串。

String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME+" WHERE "+COLUMN_ID+" = "+ '"' + id + '"';

答案 1 :(得分:-1)

getSelectedPassword正在选择没有行的赔率。不会输入do ... while构造(即cursor.moveToFirst()返回false),因此结果是返回一个空的ArrayList,其中没有元素,因此索引0超出范围。

检查ArrayList的大小,仅在大小大于0时才尝试获取密码。可能是Toast或表明错误的东西。

显然,您然后需要确定为什么找不到通过的 id ,这是根本原因。