I was implementing the following Java code in Android Studio:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(number);
...
}
This is a part of a larger application.
As you can see, I've passed only an integer value to the quantityTextView.setText(number) method.
When running the app, it crashes as soon as this method is called. Can you tell me why such a thing is happening?
Yes, use String.valueOf(), like this:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));
}
Because setText() accepts only String values or Resource ID of a String (which is infact int).
Check here: setText() Method
You can use String.valueOf(number); as input parameter of setText() or you can refer to String ID in XML with getResources().getString(R.string.number) as input value.
Convert the integer to string before putting it in the TextView:
quantityTextView.setText(Integer.toString(number));
or simply
quantityTextView.setText(number+"");
The reason your code is crashing is that setText(int) expects a resource ID. It's not very well documented, so you'd be forgiven for thinking that you could pass it an integer and have the TextView convert it to text.
You should first convert it to a String, for example with:
String.valueOf(number)
and then it will be alright.
setText() method of TextView accepts CharSequence, not integers. So, you must convert your number to String before.
Try to use this:
quantityTextView.setText(Integer.toString(x));
The reason is that, setText() only expects string or char[].
So either you can perform type casting or you can add quotes with the number
(1). by type casting
String.valueOf(number)
(2). by adding "" with the number
quantityTextView.setText(""+number);
or
quantityTextView.setText(number+"");
Related
Need help parsing, I have tried "porting" my dice roller project to Android using Android Studio, I have most of the controller values replaced with their android widget counterparts, one problem, I am not sure how to properly parse widget values to an Int. I have marked them with aligned left comments below.
modifier is an EditText
result is a TextView
I have tried many combinations and this is the most recent.
The one that worked when it was pure java was .getValue().toString().trim() but I cannot use .getValue why is this?
public void onStart()
{
super.onStart();
percentile.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v)
{
{
//issue is here
int total = Nat20_core.roll10(cumulative.isChecked(),
Integer.parseInt (String.valueOf(modifier)),
Integer.parseInt(String.valueOf(result)));
//end issue
result.setText(String.valueOf(total));
}
}
});
}
I have also tried this in a previous program as
set
This is because there is no .getValue() method for EditText and TextView widget.
For EditText, you can use getText() which returns an Editable. So, you need to get the string from it using toString(). So, you will need to use:
modifier.getText().toString();
For TextView, you can use getText() which returns a CharSequence. You also need to get the string from it using toString(). So, you can use the above line too:
result.getText().toString();
Now, you need to convert the following code:
int total = Nat20_core.roll10(cumulative.isChecked(),
Integer.parseInt (String.valueOf(modifier)),
Integer.parseInt(String.valueOf(result)));
to:
int total = Nat20_core.roll10(cumulative.isChecked(),
Integer.parseInt (modifier.getText().toString()),
Integer.parseInt(result.getText().toString()));
In EditText and TextView, the "value" is "text":
Integer.parseInt(modifier.getText().toString())
I want to show sentences for its number.
Getting number with EditText, and sentences are in string.xml
Name of strings are
sen_(number)
ex: sen_1, sen_25
I tried to make the code to String, so I did like this.
(sentence_num is getString of EditText) (sen_1 is "Hello, world!")
String getTxtString = "getText(R.string.sen_" + sentence_num + ")";
TextView scrambled_sen = (TextView)findViewById(R.id.scrambled_sentence);
scrambled_sen.setText(getTxtString);
But it shows getText(R.string.sen_1), not "Hello, world!".
How can I make it show string with its number?
I want to put getTxtString for a java code, not a String.
You could use getResources().getIdentifier() to get the string but a better and non-complicated way is through a switch case or the if else method.
Something like
If(editText value equals something){
return getString(R.string.sen_1);
}
Minimizes the errors that you could cause through the first getIdentifier() method too.
So my app needs to read text from a text box with a tag of "inText" do stuff to it (that stuff works) then write the output to a box with the id of "outView". I've been doing this with setText() and getText().
setText() was for writing the output below is what I used:
(TextView)findViewById(R.id.outView.setText(textoutput));
getText() was for reading the input text then writing it to a variable and below is what I used:
String mEdit = (EditText)findViewById(R.id.inText.getText()).toString();
You're chaining the method at the wrong spot. Remove .getText() from R.id.inText and place it after the brackets like (same thing for TextView):
String mEdit = ((EditText)findViewById(R.id.inText)).getText().toString();
Though this is an uncommon way to do thing. Rather initialize the EditText first and then get the text, it's much clearer that way:
EditText mEdit = (EditText)findViewById(R.id.inText);
String mText = mEdit.getText().toString();
You set the getText() and setText() method in wrong place.
getText() and setText() are methods of TextView and EditText classes.
But here you used it as a method of id. That's why it's showing "Can't resolve method getText/setText()". As id has no such methods.
You can do the following.
((TextView) findViewById(R.id.outView)).setText(textoutput);
and
String mEdit = ((EditText) findViewById(R.id.inText)).getText().toString();
It's not working since you need to first set the TextView:
TextView tvOut = (TextView) findViewById(R.id.outView);
TextView tvIn = (TextView) findViewById(R.id.inText);
String out = tvOut.getText().toString();
String in = tvIn.setText(out);
If you use a field check the compiler didn't automatically set it as a view instead of EditText type.
Also the casts are redundant now so you better not use them if you don't have to
I'm trying to set an int value using jTextField and the setText method. But of course setText wants a String. How do I get round this? I'll give you a snippet of the code:
private void setAllTextFields(FilmSystem e){
getFilmNameTF().setText(e.getFilmName());
lectureTF.setText(e.getLecture());
ageTF.setText(e.getAge());
priceTF.setText(e.getTicketCost());
seatsTF.setText(e.getNoOfSeats());
seatsTF is a jTextField and getNoOfSeats is a method in another class that returns a int value.
Thanks again for answering this question. Now how would I go about getting the value of the int to do something to do?
public void buyTicket() {
String newFilmName = filmNameTF.getText();
String newLecture = lectureTF.getText();
String newAge = ageTF.getText();
String newPrice = priceTF.getText();
int newSeats = seatsTF.
As you can see the code, the String values I can get easy with getText. I can then print them out or whatever with them. How can I do this with the seats int? Thanks again.
String#valueOf convert your int to String.
String.valueOf(e.getAge()); will return the string representation of the int argument.
seatsTF.setText(String.valueOf(e.Age()));
...
USe
seatsTF.setText(""+e.getNoOfSeats());
OR
seatsTF.setText(String.valueOf(e.getNoOfSeats()));
Normal ways would be
seatsTF.setText(Integer.toString(e.getNoOfSeats()));
or
seatsTF.setText(String.valueOf(e.getNoOfSeats()));
but, this can be achieved with a concatenation like this:
seatsTF.setText("" + e.getNoOfSeats());
Assuming age field is of type int, you could try something like:
ageTF.setText( Integer.toString(e.getAge()) );
Setting an int converting it to a String not a big deal. Displaying a value is a problem. To take care of how the value is displayed properly in the textfield you may use a DecimalFormat to format the numeric value. But may be the number is locale specific then you need NumberFormat instance
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMaximumIntegerDigits(12);
nf.setMaximumFractionDigits(0);
nf.setMinimumFractionDigits(0);
String s = nf.format(e.getNoOfSeats());
seatsTF.setText(s);
You may also need to read the tutorial on how to use the DecimalFormat.
To convert Integer Value to String you should
MedicineTM medicine=tblmedicine.getSelectionModel().getSelectedItem();
txtmedicine.setText(medicine.getMID());
txtDescription.setText(medicine.getDescription());
txtQty.setText(String.valueOf(medicine.getQty())); // this is what i did
cmbApproval.setValue(medicine.getApproval());
I think you should write the code as
seatsTF.setText(e.getNoOfSeats().toString());
Basically, I want an EditText in Android where I can have an integer value entered into. Perhaps there is a more appropriate object than EditText for this?
For now, use an EditText. Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())).
In the future, you might consider a NumberPicker widget, once that becomes available (slated to be in Honeycomb).
Set the digits attribute to true, which will cause it to only allow number inputs.
Then do Integer.valueOf(editText.getText()) to get an int value out.
First of all get a string from an EDITTEXT and then convert this string into integer like
String no=myTxt.getText().toString(); //this will get a string
int no2=Integer.parseInt(no); //this will get a no from the string
You can do this in 2 steps:
1: Change the input type(In your EditText field) in the layout file to android:inputType="number"
2: Use int a = Integer.parseInt(yourEditTextObject.getText().toString());