阻止 .setText 覆盖?从数据库游标结果



我正在使用带有游标的方法返回具有相同id的所有值,例如:

ID_Owner |ID_Car

1 -------- 1

1 -------- 2

有身份证的车主有1号和2号车。


使用我的方法返回这些值:

// Seleciona o Registo com o ID especeficado
public Cursor selectMotivosWhereId(long id){
    String sql = "SELECT * FROM Motivo_sintomas WHERE _ID IN (SELECT id_motivo FROM Sintoma_motivos WHERE id_sintoma = ?) " ;
    String[] args = new String[]{id + ""};
    Cursor result = this.db.rawQuery(sql, args);
    return result;
}

然后,我想在同一个文本视图中显示它们,但只显示最后一个,我认为这是因为 .setText 覆盖并且只显示最后一个。

Cursor res2 = dal.selectMotivosWhereId(position);
    res2.moveToFirst();
    while (!res2.isAfterLast()) {
        Motivo.setText(res2.getString(res2.getColumnIndex(DBContract.Motivo_sintomas.COL_Motivo)));
        res2.moveToNext();
    }

我怎样才能继续将值添加到编辑文本中并用"-"或"," 分隔它们?

非常感谢。

您必须使用 StringBuilder 并将结果附加到 StringBuilder 对象,然后将文本设置为 stringbuilder 对象。

Cursor res2 = dal.selectMotivosWhereId(position);
res2.moveToFirst();
StringBuilder sb = new StringBuilder();
while (!res2.isAfterLast()) 
{
    sb.append(res2.getString(res2.getColumnIndex(DBContract.Motivo_sintomas.COL_Motivo));
    res2.moveToNext();
}
 Motivo.setText(sb.toString());

最新更新