【发布时间】:2017-09-21 11:06:33
【问题描述】:
我有一个 ListView,我使用 toString 在 ListView 中显示我的对象。
private String displayToUI() {
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false); //if overall = "X.0", the format will print "X"
switch (FORMAT_TYPE) {
case 0: // i.e "Guy - 5 | Host | Attending"
String membership;
String attending;
if (this._playerishost == 1)
membership = "Host";
else
membership = "Guest";
if (this._playerisattending == 1)
attending = "Attending";
else
attending = "Not Attending";
return this._playername + " - " + format.format(this._playeroverall) + " | " + membership + " | " + attending;
case 1: // i.e "Guy - 5"
return this._playername + " - " + format.format(this._playeroverall);
}
return "0";
}
@Override
public String toString() {
return displayToUI();
}
如果 FORMAT_TYPE=0 ,我想使用格式,正确显示对象,所以每一列都会在他的位置,函数会像表格一样显示对象
这是现在的输出:
https://i.stack.imgur.com/HvvNH.png
编辑:
我使用了 String.format(@Pynnie 的回答),现在的代码行是:
return String.format("%-20s %-4s | %-5s | %s", this._playername, format.format(this._playeroverall), membership, attending);
但输出不合适,如下图所示:
https://i.stack.imgur.com/BEn27.png
还有,我的列表视图适配器代码:
public void editListView() {
ArrayAdapter<Player> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DBHandler.getNameListGeneral());
lv = (ListView) findViewById(R.id.db_list_view);
lv.setAdapter(arrayAdapter);
registerForContextMenu(lv);
}
【问题讨论】:
-
将您的 xml 和您的适配器代码放在这里,以便我们了解您在做什么。
-
@Rob 完成!编辑了我上面的帖子
标签: android listview format string.format