I am trying to get the string from an EditText and add 1 to the value on a button click but instead of doing 1+1 and setting it to 2, it outputs to the EditText 1111 and so on. I need some help getting the math working. Thanks in advance!
int rentPrice = 0;
int peopleCount = 0;
rentPrice = Integer.parseInt(mRentPriceText.getText().toString());
mybutton.setOnClickListent(new OnClickListener(){
mRentPriceText.setText(rentPrice+1);
}
String actually concats 1. so "1"+"1"="11".
So you have to first convert it to int and add 1 and then reconvert it to String and set it to EditText.
So change this line
mRentPriceText.setText(rentPrice+1);
to
mRentPriceText.setText(Integer.toString(rentPrice+1));
Try this.
int temp = 0;
int rentPrice = 0;
int peopleCount = 0;
rentPrice = Integer.parseInt(mRentPriceText.getText().toString());
temp =rentPrice+1
mybutton.setOnClickListent(new OnClickListener(){
mRentPriceText.setText(String.valueOf(temp));
}
Try like this:
int rentPrice = 0;
int peopleCount = 0;
mybutton.setOnClickListent(new OnClickListener(){
rentPrice = Integer.parseInt(mRentPriceText.getText().toString());
int out = rentPrice+1;
mRentPriceText.setText(String.valueOf(out));//Convert out to string
}
If you gave int value as set text then it will find some string resource so cast int to string and use inner () add +1 to rentPrice :
mRentPriceText.setText(String.valueOf((rentPrice+1)));
If you are doing mTextview.setText(someInteger) you are more likely to get the below exception
android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:1134)
at android.widget.TextView.setText(TextView.java:4940)
2
because the TextView.setText(int) makes android think that you might be referring to some R.string.some_string . So wrap up your calculation work in the String as String.valueOf("some calculation")
Also I have some Tip for you
don't use toString() in line rentPrice = Integer.parseInt(mRentPriceText.getText().toString()); otherwise you would more likely get some time NullPointerException
Instead always go with
rentPrice = Integer.parseInt(String.valueOf(mRentPriceText.getText()));
Related
I have a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));
I have 16 buttons, whose names are "button1", "button2", and so on. Is there a way I can iterate through them using a for loop, by somehow appending the number value upon each iteration? Something like this:
for(int i = 1; i<17; i++ ){
Button b = (Button)findViewById(R.id.buttoni);
I know I can simply initialize each button in my onCreate() method, but I was just curious if I could do it in a way similar to my example code.
Thank you.
You can use getIdentifier :
for(int i = 1; i<17; i++ ){
int buttonId = getResources().getIdentifier("button"+i, "id", getPackageName());
Button b = (Button)findViewById(buttonId);
//Your stuff with the button
}
You can create an array of Button's and use getIdentifier method that allows you to get an identifier by its name.
final int number = 17;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
In case someone is interested how to achive the same result using Java only
The solution above uses Android specific methods (such as getResources, getIdentifier) and can not be used in usual Java, but we can use a reflection and write a method that works like a getIdentifier:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
And then:
final Button[] buttons = new Button[17];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
NOTE:
Instead of optimizing this kind of code you should rethink your layout. If you have 17 buttons on the screen, a ListView is probably the better solution. You can access the items via index and handle onClick events just like with the buttons.
This question already has answers here:
How do I convert from int to String?
(20 answers)
Closed 9 years ago.
I have an int and I want to convert it to a string. Should be simple, right? But the compiler complains it can't find the symbol when I do:
int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);
What is wrong with the above? And, how do I convert an int (or long) to a String?
Edit: valueOf not valueof ;)
Use this String.valueOf(value);
Normal ways would be Integer.toString(i) or String.valueOf(i).
int i = 5;
String strI = String.valueOf(i);
Or
int aInt = 1;
String aString = Integer.toString(aInt);
You called an incorrect method of String class, try:
int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);
You can also do:
int tmpInt = 10;
String tmpStr10 = Integer.toString(tmpInt);
Use Integer.toString(tmpInt) instead.
I get the id of resource like so:
int test = (context.getResourceId("raw.testc3"));
I want to get it's id and put it into a string. How can I do this? .toString does not work.
String testString = Integer.toString(test);
Or you can use Use
String.valueOf()
eg.
int test=5;
String testString = String.valueOf(test);
Very fast to do it if you dnt remember or dnt have time to type long text :
int a= 100;
String s = ""+a;
What about:
int a = 100;
String s = String.format("%d", a);
I have this piece of code:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText (x);
It turns out to be an error. I know I have to change it to string, but how do I do this?
I've tried x.toString(), but it can't be compiled.
Use +, the string concatenation operator:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);
or use String.valueOf(int):
ed.setText(String.valueOf(x));
or use Integer.toString(int):
ed.setText(Integer.toString(x));
try Integer.toString(integer value); method as
ed = (EditText)findViewById(R.id.box);
int x = 10;
ed.setText(Integer.toString(x));
Try using String.format() :
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(String.format("%s",x));
ed.setText (String.ValueOf(x));
Use this in your code:
String.valueOf(x);