我有一个带有整数键和附加字符串的哈希图。 用户可以选择多个值,我希望将所有值显示在屏幕上。请参见下面的代码:
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));
}
}
当前,此代码仅将列表中的最后一个值打印到屏幕上。因此,如果用户最后选择的值是“ SelfBlaming”,则只会打印一个iwll,而其他所有先前的值都将被忽略。有没有解决这个问题的简单方法?
编辑:
1)只是为了澄清来自用户的信息是来自mysql数据库。
2)变量allOrNothing,blamingOther等的数值为1、2,以此类推,直到12。
因此,从mySQL服务器返回的数据如下:
"allOrNothing": null,
"blamingOthers": null,
"catastrophizing": null,
"emotionalReasoning": null,
"fortuneTelling": "5",
"labelling": null,
"magnifyingTheNegative": "7",
"mindReading": "8",
"minimisingThePositive": "9",
"overGeneralisation": null,
"selfBlaming": null,
"shouldStatements": null
在此示例中,用户有四个选择,因此算命,放大负数,头脑阅读和最小化正数都应显示在SetText中。
答案 0 :(得分:1)
您可以考虑使用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
答案 1 :(得分:0)
您的第一个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));
}}
}
答案 2 :(得分:0)
首先,您不需要数组。哈希映射足以存储您的键和值。
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());