阻止 .setText 覆盖?来自数据库游标结果
Stop .setText from overwriting ? From database cursor result
我正在使用带有游标的方法 return 所有具有相同 ID 的值,例如:
ID_Owner | ID_Car
1 -------- 1
1 -------- 2
车主有id,有车1和2..
用我的方法 im returning 这些值:
// 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();
}
我怎样才能继续将值添加到 edittext 并用“-”或“,”分隔它们?
非常感谢。
您必须使用 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());
我正在使用带有游标的方法 return 所有具有相同 ID 的值,例如:
ID_Owner | ID_Car
1 -------- 1
1 -------- 2
车主有id,有车1和2..
用我的方法 im returning 这些值:
// 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();
}
我怎样才能继续将值添加到 edittext 并用“-”或“,”分隔它们?
非常感谢。
您必须使用 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());