簡體   English   中英

如何修復android.database.cursorindexoutofboundsexception錯誤?

[英]how to fix the error android.database.cursorindexoutofboundsexception?

任何人? 請幫助我解決這個問題。 我收到android.database.cursorindexoutofboundsexception錯誤,但我不這樣做。 我確定我正確地命名了我的專欄,但仍然可以。 我正在嘗試做的只是獲取給定公司名稱的公司代碼。

這是我的代碼我的DatabaseAdapter

public Cursor getCompanyCode(String company)
{
    Cursor c = dbSqlite.query(Constants.DATABASE_TABLE_COMPANY,
            new String[] { Constants.DATABASE_COLUMN_ID,
                            Constants.COMPANY_CODE,Constants.COMPANY_NAME},
            Constants.COMPANY_CODE+" = ?",
            new String[]{company}, null, null, null);

    if (c != null)
    {
        c.moveToFirst();
    }
    return c;
}

這是獲取給定公司的公司代碼的另一個代碼

Cursor companyCode = databaseAdapter.getCompanyCode(company);
code = companyCode.getString(companyCode.getColumnIndex(Constants.COMPANY_CODE));

這是我的logcat。

06-04 12:54:48.085: E/AndroidRuntime(27134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uni.customercare/com.uni.customercare.ViewSummaryActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

試試這個方法:

在擴展SQLiteOpenHelper的數據庫幫助器類中定義此方法。 使用類的實例來調用此方法。

public HashMap<String, String> getCompanyDetails(){

        HashMap<String,String> company= new HashMap<String,String>();
        String selectQuery = "SELECT * FROM " + TABLE_COMPANY; //Change this query accordingly.

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        cursor.moveToFirst();
        if(cursor.getCount() > 0){
            company.put(KEY_COMP_ID, cursor.getString(0));
            company.put(KEY_COMP_CODE, cursor.getString(1));
            company.put(KEY_COMP_NAME, cursor.getString(2));

        }
        cursor.close();
        db.close();

        return company;
    }

使用游標companyCode的簡單方法。

companyCode.moveToFirst();
while( !companyCode.isAfterLast() ) {
    //do something with companyCode..
    //.....
    companyCode.moveToNext();
}
companyCode.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM