仅将姓氏名称而不是所有唯一类别添加到TextView中

Set<String> uniqueCategories; // global

......

uniqueCategories = new TreeSet<>();

for(Checkout c : checkOutArrayList) {
    uniqueCategories.add(c.getCategory());
}

for (String strGlobalCategory : uniqueCategories) {
    System.out.println("Unique:"+strGlobalCategory);
    textVisible.setText(strGlobalCategory); // getting name of last Category only
    }


当我在每个循环中使用textVisitCharges.setText(strGlobalCategory);时,无法将strGlobalCategory解析为变量

最佳答案

您应该将所有类别附加到单个String并将结果分配给TextView

StrinbBuilder text = new StringBuilder();
boolean first = true;
for (String strGlobalCategory : uniqueCategories) {
    if (!first) {
        text.append(", ");
    }
    first = false;
    text.append (strGlobalCategory);
}
textVisible.setText(text.toSTring());

07-24 09:17