我正在尝试在视图中显示 SQLite 表。我希望我的第一列的宽度为 20 个字符。我试图通过使用 substring 方法来实现此效果,但我得到 stringindexoutofboundsexceptions 字符串少于 20 个字符。任何帮助是极大的赞赏。这是我的代码:
在视图中显示表格的方法:
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iName).substring(0, 20) + " " + c.getString(iQuantity) + " " + c.getString(iValue) + "\n";
}