从哈希图打印一个值到屏幕-一次打印多个值?

AndroidNewbee:

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

  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中。

Amr Saleh:

首先,您不需要数组。哈希映射足以存储您的键和值。

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());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章