0

I am having a problem displaying data from my DB in a list view. the below is the code from for my display method.

Display

public ArrayList<Object> display(String rows) {

    ArrayList<Object> rowArray = new ArrayList<Object>();

    String[] columns = new String[] { KEY_ROWID, KEY_PACKNAME,
            KEY_PACKRENT, KEY_SIMRATE, KEY_NATMINSBUN, KEY_INTMINSBUN,
            KEY_INTMINSRATE, KEY_NATSMSBUN,
                            KEY_NATSMSRATE, KEY_INTSMSBUN, KEY_INTSMSRATE, 
                            KEY_MMSBUN, KEY_MMSRATE, KEY_DATALBUN,
            KEY_DATALRATE };
    Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null, null,
            null, null, null);

    cursor.moveToFirst();


    if (!cursor.isAfterLast())
    {
        do
        {   
            rowArray.add(cursor.getLong(0));
            rowArray.add(cursor.getString(1));
            rowArray.add(cursor.getString(2));
            rowArray.add(cursor.getString(3));
            rowArray.add(cursor.getString(4));
            rowArray.add(cursor.getString(5));
            rowArray.add(cursor.getString(6));
            rowArray.add(cursor.getString(7));
            rowArray.add(cursor.getString(8));
            rowArray.add(cursor.getString(9));
            rowArray.add(cursor.getString(10));
            rowArray.add(cursor.getString(11));
            rowArray.add(cursor.getString(12));
            rowArray.add(cursor.getString(13));
            rowArray.add(cursor.getString(14));

    }         while (cursor.moveToNext());

    cursor.close();
    }

    return rowArray;


}

This is the Arraylist

            EtiTariffDatabase test = new EtiTariffDatabase(this);

    TextView TV = (TextView) findViewById(R.id.s_etipackname);
    TextView TV1 = (TextView) findViewById(R.id.s_etipackrent);
    TextView TV2 = (TextView) findViewById(R.id.srentalrate);
    TextView TV3 = (TextView) findViewById(R.id.snationalbun);
    TextView TV4 = (TextView) findViewById(R.id.seintbun);
    TextView TV5 = (TextView) findViewById(R.id.senatsmsbun);
    TextView TV6 = (TextView) findViewById(R.id.seintsmsbun);
    TextView TV7 = (TextView) findViewById(R.id.sedatalocalbun);


    test.open();

    try
    {
    ArrayList<Object> row = test.display(null);



    TV.setText((String)row.get(1));
    TV1.setText((String)row.get(2));
    TV2.setText((String)row.get(3));
    TV3.setText((String)row.get(4));
    TV4.setText((String)row.get(6));
    TV5.setText((String)row.get(8));
    TV6.setText((String)row.get(10));
    TV7.setText((String)row.get(12));


    }
    catch (Exception e)
    {
        Log.e("Retrieve Error", e.toString());
        e.printStackTrace();
    }
    test.close();


}

the exception I catch is:

04-07 16:48:23.759: E/Retrieve Error(4134): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 04-07 16:48:23.829: W/System.err(4134): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 04-07 16:48:23.829: W/System.err(4134): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

I have been trying to play around for a long time. It worked initially but I deleted the DB and recreated it. From then no data gets displayed with me having the above error. Any assistance would be greatly appreciated.


Do you have any data-rows inside your database? From what the Exception says you try to access a field in the array but the array itself is empty.

Everything looks like it should work, but when there are no data in the database the function display will return an ArrayList that is empty (row.size should be 0 then).

So test for emptyness:

try
{
    ArrayList<Object> row = test.display(null);

    if(row.size != 0) {
        TV.setText((String)row.get(1));
        TV1.setText((String)row.get(2));
        TV2.setText((String)row.get(3));
        TV3.setText((String)row.get(4));
        TV4.setText((String)row.get(6));
        TV5.setText((String)row.get(8));
        TV6.setText((String)row.get(10));
        TV7.setText((String)row.get(12));
    }   
} catch (Exception e) {
    Log.e("Retrieve Error", e.toString());
    e.printStackTrace();
}
test.close();
4

2 回答 2

0

04-07 16:48:23.759: E/Retrieve Error(4134): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0

That clearly indicates that your method returns an empty ArrayList. Indexes start from 0 and not 1 (which is not related to your error, but you should correct that too). If there is no data in your table the ArrayList will not contain any elements.

You are better off returning a value object instead of individually adding each column to an ArrayList (again, not related to your error but this is a good practice).

于 2013-04-07T17:21:02.200 回答
0

您的数据库中有任何数据行吗?根据异常所说,您尝试访问数组中的字段,但数组本身为空。

一切看起来应该可以工作,但是当数据库中没有数据时,该函数display将返回一个空的 ArrayList(row.size那么应该为 0)。

所以测试空性:

try
{
    ArrayList<Object> row = test.display(null);

    if(row.size != 0) {
        TV.setText((String)row.get(1));
        TV1.setText((String)row.get(2));
        TV2.setText((String)row.get(3));
        TV3.setText((String)row.get(4));
        TV4.setText((String)row.get(6));
        TV5.setText((String)row.get(8));
        TV6.setText((String)row.get(10));
        TV7.setText((String)row.get(12));
    }   
} catch (Exception e) {
    Log.e("Retrieve Error", e.toString());
    e.printStackTrace();
}
test.close();
于 2013-04-07T17:19:12.463 回答