我在列表视图中显示来自我的数据库的数据时遇到问题。以下是我的显示方法的代码。
显示
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;
}
这是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();
}
我抓到的例外是:
04-07 16:48:23.759:E / Retrieve Error(4134):java.lang.IndexOutOfBoundsException:索引1无效,大小为0 04-07 16:48:23.829:W / System.err(4134):java.lang.IndexOutOfBoundsException:索引1无效,大小为0 04-07 16:48:23.829:W / System.err(4134):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
我一直试图玩很长时间。它最初工作,但我删除了数据库并重新创建它。从那以后,没有数据显示我有上述错误。非常感谢任何帮助。
答案 0 :(得分:0)
您的数据库中是否有任何数据行?根据Exception的说法,您尝试访问数组中的字段,但数组本身是空的。
一切看起来都应该有效,但是当数据库中没有数据时,函数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();
答案 1 :(得分:0)
04-07 16:48:23.759:E / Retrieve Error(4134):java.lang.IndexOutOfBoundsException:索引1无效,大小为0
这清楚地表明您的方法返回空ArrayList
。索引从0开始而不是1(这与您的错误无关,但您也应该更正)。如果表中没有数据,则ArrayList将不包含任何元素。
您最好退回value object,而不是单独将每列添加到ArrayList
(再次,与您的错误无关,但这是一个很好的做法)。