简体   繁体   中英

printing a value to screen from hashmap - print multiple values at once?

I have a hashmap with an integer key and a string attached. The user can have multiple values selected and I would like all values to be displayed to screen. Please see the code below:

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

        }

    }

Currently this code will only print the last value in the list to screen. So if the users last selected value was "SelfBlaming", only that one iwll get printed and all other previous values get ignored. Is there a simple way around this?

EDIT:

1) Just to clarify the information from the user is coming from a mysql database.

2) The variables allOrNothing, blamingOthers etc have a numeric value of 1, 2, etc up the 12.

So the data returned from the mySQL server looks like this:

     "allOrNothing": null,
     "blamingOthers": null,
     "catastrophizing": null,
     "emotionalReasoning": null,
     "fortuneTelling": "5",
     "labelling": null,
     "magnifyingTheNegative": "7",
     "mindReading": "8",
     "minimisingThePositive": "9",
     "overGeneralisation": null,
     "selfBlaming": null,
     "shouldStatements": null

From this example the user has four selections so, fortune Telling, magnifying the negative, mind reading and minimising the positive should all be displayed in the SetText.

You might consider using a Java "enum":

Sample code:

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

}

Sample output:

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

Your first foor loop end without nothing but giving twistedThinking value as last key. Use this. I think you will understand.

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

        }}

    }

First of all, you do not need an array. The Hash map should suffice for storing your keys and values.

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

Then if you need to know the items the user has selected then you will need to keep such information. This can be done in many several ways but a very simple one is to simply keep track of the selected keys in a set. You can simply add or remove keys from this set as the user selects or unselects keys.

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

Lastly now to print all the selected thoughts you will need to get all the selected ones and concatenate them in a string first before adding them to your text view.

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

DistortionLogDetails.setText(selectedThoughtsSB.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM