-1

I know this is a simpleton question, but I am not going to school for java, just learning it online.

how to a have a textview with an initial value of 0. and then everytime you press a button it ads 25 points to the score board.

At first I wanted the button press to add a random number between 42-57 to the score board.

And then how to do convert that int or long to a string to make it fit into a textview and keep the current score, and then add a new score.

EDIT: ok so someone said I should post the code so here it is.. where do i put this..

TextView txv182 = (TextView) findViewById(R.id.welcome);
txv182.setText(toString(finalScore));

Because when I do it, I get an error: The method toString() in the type Object is not applicable for the arguments (int)


public class MainActivity extends Activity {

// Create the Chartboost object
private Chartboost cb;

MediaPlayer mp = new MediaPlayer();
SoundPool sp;
int counter;
int db1 = 0;

Button bdub1;



TextView txv182;

int finalScore;





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


    txv182 = (TextView) findViewById(R.id.welcome);

    finalScore = 100;




    sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    db1 = sp.load(this, R.raw.snd1, 1);

    bdub1 = (Button) findViewById(R.id.b4DUB1);



    bdub1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (db1 != 0)
                sp.play(db1, 1, 1, 0, 0, 1);
            txv182.setText(finalScore);

        }
    });
  • Please show us what you have tried already (some code would be great). If you haven't tried anything yet, or you don't know where to start, you need to learn more Java. – supersam654 Jun 01 '13 at 19:28
  • Have you tried anything yet? Start with a simple project with just a button, etc. and add pieces as you get them to work. Sorry, but you can't just post a project requirement to SO and expect other users to write code for you (without paying for it, that is). – Jonathon Reinhart Jun 01 '13 at 19:28

2 Answers2

2

First you need to store your score to certain integer variable say score and set it to any initial value you want and use.

TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(toString(score));

you do not need to initialize the textview with value just in onclick() of button do score+=25and add text to your textview as above.

hope this helps

karan
  • 8,637
  • 3
  • 41
  • 78
  • and i thought int only held -128 through 128. wouldnt i need to use long incase the score goes over... 130? – Brendan Carr Jun 01 '13 at 20:13
  • `int` stores values from -2,147,483,648 to 2,147,483,647 (inclusive). first page after you type "java int" to google – alex Jun 01 '13 at 20:38
  • it's still not working. i keep getting this error: "The method toString() in the type Object is not applicable for the arguments (int)" – Brendan Carr Jun 01 '13 at 20:54
  • just try to use tv.setText(score); – karan Jun 01 '13 at 20:59
  • or you might be needing to create score as String object and to use its value you need to convert string to integer using Integer.parseInt(score)...i think this is better one – karan Jun 01 '13 at 21:01
  • @BrendanCarr : edit your question with your code so its easy to understand – karan Jun 01 '13 at 22:22
  • ok i've been banned from asking questions because apparently i didn't put enough information. So can i please get some help with some up votes and how do i update the score variable and convert it to string so that the textview can show the new score.. – Brendan Carr Jun 02 '13 at 04:58
0

It's actually like this..

pernts+=25;

txv182.setText(String.valueOf(pernts));

in android, after the

      public class MainActivity extends Activity {

but before..

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

i wrote..

int pernts;
String strI;

so then after the above @Override majigy, i wrote..

strI = "" + pernts;
pernts = 0;

because when i wrote it like.. int pernts = 0; it never worked, forcing me to add final and what not..

any way.. How to convert an integer value to string? anSwered the question..

and so the end was like this

pernts+=25;
txv182.setText(String.valueOf(pernts));

i figured out you have to add a value to the int variable not the string.. i kept wanting to add 25 and i was getting 25252525252525.., instead of 25 50 75 100.. kinda cool actually.. separating the logic of how to "add" 25 .. anyway thanks.. SOF!

30 minutes later... found this.. How can I generate random number in specific range in Android? ..

    import android.app.Activity;

      public class InsMenu extends Activity {

static TextView txv182;

static int pernts;


    Button bdub1;

    public static void addPernts() {

    int min = 37;
    int max = 77;

    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;

    pernts+=i1;

    txv182.setText(String.valueOf(pernts));

}


    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.BLAHBLAH);

   bdub1 = (Button) findViewById(R.id.b4DUB1);
   txv182 = (TextView) findViewById(R.id.welcome);

    bdub1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            addPernts();
        }

--- i created my first objekt thanks to this too.. http://www.tutorialspoint.com/java/java_methods.htm ..

Community
  • 1
  • 1