samedi 17 décembre 2016

Android - using setText with hashMap to output random key values

I am trying to make a simple application that will output the value of the key when the random key is selected when the button I have is clicked. So far, I have done this:

private Button showMeButton;
private TextView showTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    showMeButton = (Button) findViewById(R.id.buttonId);
    showTextView = (TextView) findViewById(R.id.textViewId);

    final HashMap mountainMap = new HashMap();
    mountainMap.put("Everest",1200);
    mountainMap.put("Kilimanjaro",1000);
    mountainMap.put("Makulu",3000);
    mountainMap.put("Kamet",500);
    mountainMap.put("Trivor",1000);
    mountainMap.put("K12",800);
    mountainMap.put("Ultar",100);
    mountainMap.put("Mana",900);


    showMeButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            //handle the events when the button is clicked

            //generate a random number
            Random rand = new Random();
            //random number will be between 0 and num elements in mountain.
            int randomNum = rand.nextInt(mountainMap.size());

            //this gives me an error
            showTextView.setText(mountainMap.get(randomNum));

        }
    });
}

So whenever I click my "Generate random mountain value" button, I am trying to access the value of the randomly selected key. When using

showTextView.setText(mountainMap.get(randomNum));

I get an error:

cannot resolve method "setText(java.lang.Object)"

How could I go about resolving this?




Aucun commentaire:

Enregistrer un commentaire