对于每个循环获取最后一个记录名称到TextView

时间:2015-07-22 07:31:11

标签: java android foreach

在TextView

中仅获取最后一个类别名称而不是所有唯一类别
textVisitCharges.setText(strGlobalCategory);

当我使用strGlobalCategory时,每个循环都无法将string folderName = "[Gmail]/Yıldızlı"; Mailbox inbox = imap.SelectMailbox(folderName); 解析为变量

3 个答案:

答案 0 :(得分:2)

您应该将所有类别附加到单个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());

答案 1 :(得分:1)

setText始终将旧文本替换为当前文本。

您可以使用append查看所有类别。

for (String strGlobalCategory : uniqueCategories) {
    System.out.println("Unique:"+strGlobalCategory);
    textVisible.append(strGlobalCategory+", "); // Now you will have all categories
}

答案 2 :(得分:0)

        Set<String> uniqueCategories; // global
         //Suppose your set of string is like that
        String checkOutArrayList[]={"c","e","i","f","a","g","b","d","j","h"};
        uniqueCategories = new TreeSet<String>();

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

        StringBuilder sb=new StringBuilder();
        sb.append("");//This blank text does not show null when set is empty
        for (String strGlobalCategory : uniqueCategories) {
            sb.append(strGlobalCategory);
            //sb.append(strGlobalCategory+" ");
            //sb.append(strGlobalCategory+"\n");
            //sb.append(strGlobalCategory+"\t");
        }


        //If you want to write the text without space your string is abcdefghij
        //If you want to write the text with space your string is a b c d e f g h i j
        //If you want to write the text with next line your string is 
        /*
            a
            b
            c
            ...
         */
        //If you want to write the text with tab your string is a   b   c   d   e   f......

        //In this way you can set all the set element with different ways...
          textVisible.setText(sb);

        //Your all set element is not appendign because the value is updated each time and the is showing