-1

Good day programming warriors, please help me to this simple problem of mine, i'm new to android. How can I How to convert TextView value to Integer. please see my attached codes, thank you.

public class appleTrivia extends AppCompatActivity {

     public int total = 0;
     public int score = 40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple_trivia);


        TextView scoreLabel = (TextView) findViewById(R.id.labelScore);
        scoreLabel.setText("Score: " + score);
        scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));

        try {
            total = Integer.parseInt("score: " + scoreLabel);
        }
        catch (NumberFormatException nfe )
        {
        }
    }
        public void onClickProceed (View view) {
        Intent intent = new Intent(this, cherry.class);
        startActivity(intent);
        Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show();
    }
}

I need to convert scoreLabel value to integer so I can add it to another score, thank you. PS. Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show(); give's me 0 score.

kiran kumar
  • 1,402
  • 17
  • 34

2 Answers2

1

First, you need to remove the Score : text using regex maybe :

String str = scoreLabel.getText().toString();      
str = str.replaceAll("[^-?0-9]+", "");

Then parse it to integer :

int total = Integer.parseInt(str);

EDIT

public class appleTrivia extends AppCompatActivity {

     public int total = 0;
     public int score = 40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple_trivia);


        TextView scoreLabel = (TextView) findViewById(R.id.labelScore);
        scoreLabel.setText("Score: " + score);
        scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));

        try {
            String str = scoreLabel.getText().toString();      
            str = str.replaceAll("[^-?0-9]+", "");
            total = Integer.parseInt(str);
        }
        catch (NumberFormatException nfe )
        {
        }
    }
        public void onClickProceed (View view) {
        Intent intent = new Intent(this, cherry.class);
        startActivity(intent);
        Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show();
    }
}
Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
0

Try to set the Value in total and just display your result with a different string, if you want to add the int to another one you can use total and display it again.

public class appleTrivia extends AppCompatActivity {

     public int total = 0;
     public int score = 40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apple_trivia);


        TextView scoreLabel = (TextView) findViewById(R.id.labelScore);
        scoreLabel.setText("Score: " + score);
        scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));

        try {
        total = Integer.parseInt(scoreLabel.getText().ToString());
        String totalScore = "Score:" + Total; 
        }
        catch (NumberFormatException nfe )
        {
        }
    }
        public void onClickProceed (View view) {
        Intent intent = new Intent(this, cherry.class);
        startActivity(intent);
        Toast.makeText(appleTrivia.this, "Your score is:" + totalScore, Toast.LENGTH_LONG).show();
    }
}
Anasbzr
  • 59
  • 2
  • 10
  • Sir this code gave me "Your score is: null" on toast message. – Herbert Willkomm Dec 08 '15 at 06:12
  • sorry i didn't get back to you any earlier. i don't know if you have fixed your issue. If not but apparently there is no function where you are adding the scores to total. and you have set the variable to be 0 therefore it shows null. – Anasbzr Dec 11 '15 at 00:41