How To Convert Textview Value To Integer
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,
Solution 1:
First, you need to remove the Score :
text using regex maybe :
Stringstr = scoreLabel.getText().toString();
str = str.replaceAll("[^-?0-9]+", "");
Then parse it to integer :
int total = Integer.parseInt(str);
EDIT
publicclassappleTriviaextendsAppCompatActivity {
publicinttotal=0;
publicintscore=40;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple_trivia);
TextViewscoreLabel= (TextView) findViewById(R.id.labelScore);
scoreLabel.setText("Score: " + score);
scoreLabel.setText("Score: " + getIntent().getExtras().getInt("points", 0));
try {
Stringstr= scoreLabel.getText().toString();
str = str.replaceAll("[^-?0-9]+", "");
total = Integer.parseInt(str);
}
catch (NumberFormatException nfe )
{
}
}
publicvoidonClickProceed(View view) {
Intentintent=newIntent(this, cherry.class);
startActivity(intent);
Toast.makeText(appleTrivia.this, "Your score is:" + total, Toast.LENGTH_LONG).show();
}
}
Solution 2:
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();
}
}
Post a Comment for "How To Convert Textview Value To Integer"