java - 来自 map 的 TextView

标签 java dictionary collections javafx

我有一个 Map<String, Integer> 我需要将所有值打印到 textView 中。 我使用了这段代码,但它只打印最后一个值,而不是全部

int count = 0;
for(Map.Entry<String, Integer> entry : list){
    count++;
    textView.setText(count + "   " + entry.getKey()+"    "+entry.getValue());
}

我做错了什么?

最佳答案

每次迭代都会覆盖文本。

您需要将每个值附加到一个字符串。

例如。

int count = 0;
StringBuilder builder = new StringBuilder();

for(Map.Entry<String, Integer> entry:list) {
    count++;
    builder.append(count + "   " + entry.getKey()+"    "+entry.getValue());
} 

textView.setText(builder.toString());

关于java - 来自 map 的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438569/

相关文章:

python - 在python中定义多维字典的最佳方法?

Swift: '(String) -> AnyObject?' 没有名为 'subscript' 的成员

java - 检测数组列表中包含多个员工对象的重复电子邮件 ID

c++ - 非平凡对象的 ATL 集合

java - 如何在无空设计中实现List、Set、Map?

java - JSP beans - 每次加载页面时都读取数据库吗?

java - 如何在Java中生成随机正数和负数

java - Bouncy CasSTLe CMS 公钥加密

java - 字符串初始化和性能

python - 通过相同的键连接大型字典