我想在视图中显示一个SQLite表。我希望我的第一列有20个字符的宽度。我试图通过使用子字符串方法来实现这种效果,但是我得到字符串少于20个字符的stringindexoutofboundsexceptions。任何帮助是极大的赞赏。这里是我的代码:是否可以在SQLite表中设置列宽?
在视图中显示表方法:
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";
}
限制表格字段中的字符数有什么好处吗? –
@LazyNinja Im显示三列,我想对齐列,使其看起来有组织,并留下接下来的两列的空间。我的字符串范围从9个字符到25个字符。 –
为什么你有可以浪费5个字符的列?你也可以使用[TextView#setEms()](http://developer.android.com/reference/android/widget/TextView.html#setEms%28int%29)/ [TextView.html#setEllipsize()](http ://developer.android.com/reference/android/widget/TextView.html#setEllipsize%28android.text.TextUtils.TruncateAt%29)在显示期间限制文本 – zapl