Java 从hashmap打印一个值到屏幕-一次打印多个值?

Java 从hashmap打印一个值到屏幕-一次打印多个值?,java,android,hashmap,Java,Android,Hashmap,我有一个带有整数键和字符串的hashmap。 用户可以选择多个值,我希望所有值都显示在屏幕上。请参阅下面的代码: Integer[] twistedThinking = {allOrNothing, blamingOthers, catastrophizing, emotionalReasoning, fortuneTelling, labelling, magnifying, mindReading, minimising, overGeneralisation, selfBlam

我有一个带有整数键和字符串的hashmap。 用户可以选择多个值,我希望所有值都显示在屏幕上。请参阅下面的代码:

  Integer[] twistedThinking = {allOrNothing, blamingOthers, catastrophizing, emotionalReasoning, fortuneTelling,
    labelling, magnifying, mindReading, minimising, overGeneralisation, selfBlaming, shouldStatements};

    for(Integer key: twistedThinking){
        key=twistedThought;
    }

    Map<Integer, String> twistedThoughtsMap = new HashMap<>();
    twistedThoughtsMap.put(allOrNothing, "All or Nothing Thinking");
    twistedThoughtsMap.put(blamingOthers, "Blaming Others");
    twistedThoughtsMap.put(catastrophizing, "Catastrophizing");
    twistedThoughtsMap.put(emotionalReasoning, "Emotional Reasoning");
    twistedThoughtsMap.put(fortuneTelling, "Fortune Telling");
    twistedThoughtsMap.put(labelling, "Labelling");
    twistedThoughtsMap.put(magnifying, "Magnifying the Negative");
    twistedThoughtsMap.put(mindReading, "Mind Reading");
    twistedThoughtsMap.put(minimising, "Minimising the Positive");
    twistedThoughtsMap.put(overGeneralisation, "Over Generalisation");
    twistedThoughtsMap.put(selfBlaming, "Self-Blaming");
    twistedThoughtsMap.put(shouldStatements, "Should Statements");

    // Enhanced for loop searches the HashMap for any keys and sets the appropriate background.
    for (Integer key : twistedThinking) {
        if (twistedThought == key) {
            DistortionLogDetails.setText(twistedThoughtsMap.get(key));

        }

    }

从这个例子中,用户有四个选择,因此,算命、放大负数、读心术和最小化正数都应该显示在SetText中。

您的第一个foor循环结束时,除了给出twistedThinking值作为最后一个键外,什么都没有。用这个。我想你会理解的

   Integer[] twistedThinking = {allOrNothing, blamingOthers, catastrophizing, emotionalReasoning, fortuneTelling,
    labelling, magnifying, mindReading, minimising, overGeneralisation, selfBlaming, shouldStatements};



    Map<Integer, String> twistedThoughtsMap = new HashMap<>();
    twistedThoughtsMap.put(allOrNothing, "All or Nothing Thinking");
    twistedThoughtsMap.put(blamingOthers, "Blaming Others");
    twistedThoughtsMap.put(catastrophizing, "Catastrophizing");
    twistedThoughtsMap.put(emotionalReasoning, "Emotional Reasoning");
    twistedThoughtsMap.put(fortuneTelling, "Fortune Telling");
    twistedThoughtsMap.put(labelling, "Labelling");
    twistedThoughtsMap.put(magnifying, "Magnifying the Negative");
    twistedThoughtsMap.put(mindReading, "Mind Reading");
    twistedThoughtsMap.put(minimising, "Minimising the Positive");
    twistedThoughtsMap.put(overGeneralisation, "Over Generalisation");
    twistedThoughtsMap.put(selfBlaming, "Self-Blaming");
    twistedThoughtsMap.put(shouldStatements, "Should Statements");

    // Enhanced for loop searches the HashMap for any keys and sets the appropriate background.
    for (Integer key : twistedThoughtsMap) {
       for(Integer key1: twistedThinking){
        key1=twistedThought;
        if (twistedThought == key) {
            DistortionLogDetails.setText(twistedThoughtsMap.get(key));

        }}

    }
Integer[]twistedThinking={不允许任何事情,责备他人,灾难,情感推理,算命,
标签、放大、读心术、最小化、过度概括、自责、应该陈述};
Map twistedThoughtsMap=new HashMap();
扭曲的思想(不允许“全有或全无思想”);
扭曲的想法(责备他人,“责备他人”);
扭曲的思维方式(灾难化,“灾难化”);
扭曲的思维方式(情感推理,“情感推理”);
扭曲的思维方式(算命术,算命术);
扭曲的思维方式(贴标签,“贴标签”);
扭曲的思想映射(放大,“放大负片”);
扭曲的思维方式(读心术,“读心术”);
扭曲思想映射(最小化,“最小化正面”);
扭曲的思维方式(过度概括,“过度概括”);
扭曲的思维方式(自我责备,自我责备);
扭曲的思想映射(shouldStatements,“shouldStatements”);
//增强for循环在HashMap中搜索任何键并设置适当的背景。
for(整数键:twistedThoughtsMap){
for(整数键1:twistedThinking){
键1=扭曲的想法;
if(twistedthough==键){
扭曲logdetails.setText(twistedThoughtsMap.get(key));
}}
}

<代码> > p>您可以考虑使用java“枚举”:

示例代码:

package com.example.cbt;

public class CBT {

    public enum TwistedThinking {
        allOrNothing, blamingOthers, catastrophizing, emotionalReasoning, fortuneTelling,
        labelling, magnifying, mindReading, minimising, overGeneralisation, selfBlaming,
        shouldStatements
    };


    public static void main(String[] args) {
        for (TwistedThinking thought : TwistedThinking.values()) {
            System.out.println("Thought(" + thought.ordinal() + ")=" + thought.name());
        }
    }

}
样本输出:

Thought(0)=allOrNothing
Thought(1)=blamingOthers
Thought(2)=catastrophizing
Thought(3)=emotionalReasoning
Thought(4)=fortuneTelling
Thought(5)=labelling
Thought(6)=magnifying
Thought(7)=mindReading
Thought(8)=minimising
Thought(9)=overGeneralisation
Thought(10)=selfBlaming
Thought(11)=shouldStatements

首先,您不需要数组。哈希映射应该足以存储密钥和值

Map<Integer, String> twistedThoughtsMap = new HashMap<>();
    twistedThoughtsMap.put(allOrNothing, "All or Nothing Thinking");
    twistedThoughtsMap.put(blamingOthers, "Blaming Others");
    // Add the rest of your data...

变量应以小写字符开头<代码>扭曲日志细节.setText(twistedThoughtsMap.get(key))
TextView
提供了一种
append(“abc”)
-method,那么如果mysql序号从1开始呢?只需“+1”枚举(从零开始)序号!并为自己节省大量代码/大量麻烦:)尝试为每个循环更新嵌套,但它仍然只打印最后的字符串“shouldstatements”。信息来自MYSQL数据库。IDE要求从您建议的集合中获取两个参数。我通过将hashmap更改为hashSet实现了您的答案,但是它会打印出用户所选集合的整数值。它几乎起作用了。打印出的5,6,7,8是hashmap中的整数值,而不是与之相关的字符串。关于HashSet的初始化,我的语法有一个输入错误。我现在修好了。关于产出。您确定要追加的是
twistedThoughtsMap.get(key)
,而不仅仅是
key
Map<Integer, String> twistedThoughtsMap = new HashMap<>();
    twistedThoughtsMap.put(allOrNothing, "All or Nothing Thinking");
    twistedThoughtsMap.put(blamingOthers, "Blaming Others");
    // Add the rest of your data...
Set<Integer> selectedSet = new HashSet<>();
// I will assume you have some kind of call back to tell you the user selected one or unselected (Since your question is missing how you get this info)
private void selectThought(Integer thoughtKey) {
    selectedSet.add(thoughtKey);
}

private void unselectThought(Integer thoughtKey) {
    selectedSet.remove(thoughtKey);
}
StringBuilder selectedThoughtsSB = new StringBuilder();
for(Integer key : selectedSet) {
    selectedThoughtsSB.append(twistedThoughtsMap.get(key) + "\n");
}

DistortionLogDetails.setText(selectedThoughtsSB.toString());