bribrem
bribrem

Reputation: 37

Display a random integer in an EditText box (or another box if I am mistaken)

I am trying to make an app in which upon clicking a button 3 things happen, 1) I hear a click (works) 2) I generate a random number (may or may not work) 3) The random number is displayed My code is the following:

    package arkham.test2;

import java.util.Random;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;

public class Arkhamtest2Activity extends Activity {
    EditText randomN;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final MediaPlayer mpClick = MediaPlayer.create(this, R.raw.click);
        randomN = (EditText) findViewById(R.id.randomN);

        //button 1 start        
        Button bMythos = (Button) findViewById(R.id.mythos);
        bMythos.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpClick.start();
                Random r = new Random();
                int n=r.nextInt(6) + 1;
                randomN.setText(n);
            }
        });
        //button 1 end
    }

}

When I click the button I hear the click but nothing else happens. I don't know if a random number is generated as none is displayed. What do I need to place in the brackets of randomM.set() I tried n and nothing happens. I tried "n" and got the letter n (was expected) not sure what else to try...

Upvotes: 1

Views: 859

Answers (1)

ngesh
ngesh

Reputation: 13501

try..

randomN.setText(String.valueOf(n));

This happens because when you pass an int value to this method .. it assumes that its a resource id and not the value you want to set as Text. so no way you can do that...

Upvotes: 4

Related Questions