基本上它是一个 Android 应用程序,用户完成了一些心理健康日志,就像乳制品一样。其中一个屏幕让用户从“12种扭曲思维”中进行选择。用户可以进行多项选择。这12种类型存储在mysql数据库中,主键为1-12。这就是背景故事。
上面的代码来自一个允许用户查看其日志的类。当他们打开日志时,它会从 mysql 服务器提取数据并将其显示在 android 内的 textView 中。如果用户选择了前 3 种扭曲思维,那么数据库将返回 1、2、3、null、null、null、null...等等,最多 12。所以我得到的数据集看起来像这样。然后,我将这些数字 1、2、3 分配给 java 类中扭曲思维变量的名称。这镜像了数据库。 1 = 全有或全无,2 = 指责他人......等等,最多 12 = 应该陈述。
下面的代码有效。然而,它每次都会向列表中添加一个额外的选择。例如。如果用户只做出一项选择 - 说“allOrNothing”。它将“全部或无”打印到 TextView ,但它还会添加“Should statements”。如果用户选择“全有或全无”和“应该陈述”,它将在列表中添加“自责”。我不知道这个幻象选择是从哪里来的,也不知道为什么。有人可以检查下面的代码并帮助我查明它。
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");
Set<Integer> selectedSet = new HashSet<>(); for (Integer keys : twistedThinking) {
if (twistedThinking != null) {
selectedSet.add(keys);
} }
StringBuilder selectedThoughtsSB = new StringBuilder();
for(Integer key : selectedSet) {
selectedThoughtsSB.append(twistedThoughtsMap.get(key) + "\n");
} distortionLogDetails.setText(selectedThoughtsSB.toString());
}
这是从mysql服务器返回到Android的数据示例。
"allOrNothing": null,
"blamingOthers": null,
"catastrophizing": null,
"emotionalReasoning": null,
"fortuneTelling": "5",
"labelling": null,
"magnifyingTheNegative": "7",
"mindReading": "8",
"minimisingThePositive": "9",
"overGeneralisation": null,
"selfBlaming": null,
"shouldStatements": null
最佳答案
您应该检查keys != null
,而不是twistedThinking != null
。您获得的额外 HashMap
值用于键 null
。下面的代码应该可以工作。
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");
Set<Integer> selectedSet = new HashSet<>();
for (Integer keys : twistedThinking) {
if (keys != null) {
selectedSet.add(keys);
} }
StringBuilder selectedThoughtsSB = new StringBuilder();
for(Integer key : selectedSet) {
selectedThoughtsSB.append(twistedThoughtsMap.get(key) + "\n");
} distortionLogDetails.setText(selectedThoughtsSB.toString());
}
关于java - 调试 HashMap - Java Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57083883/