我有一个带有整数键和字符串的哈希图。用户可以选择多个值,我希望所有的值都显示在屏幕上。请参考下面的代码:
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",则只会打印这一个值,而忽略所有其他先前的值。有什么简单的方法可以解决这个问题吗?
编辑:
1)只是为了澄清来自用户的信息来自mysql数据库。
2)变量allOrNothing、blamingOthers等的数值为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中。
发布于 2019-07-17 00:23:10
首先,您不需要数组。Hash map应该足以存储您的键和值。
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());发布于 2019-07-17 00:19:11
您可以考虑使用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发布于 2019-07-16 23:56:51
您的第一个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));
}}
}https://stackoverflow.com/questions/57061051
复制相似问题