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())
Related
I am pulling data from a Website to my application. I want the TextView to display the result I want from the website immediately as the user launches the app. However html codes make the result look weird some times and I am trying to correct it. I have the codes that will do what I am trying to do. I just can't figure out how to get it to do everything automatically at app launch. It needs to pull the code from the website and if it receives any special symbols within the string I want it to correct it as soon as the app launches. Here is an example...
TextView tv = (TextView) findViewbyId(R.id.my_textview_result);
tv.setText(resultFromWebsite);
The result it pulled: You u0026 Me Forever!
The result I want: You & Me Forever! My app should correct that.
Here is my correction code...
public void symbolTextFilter(TextView myTv) {
String getData = tv.getText().toString();
if (getData.contains("u0026") {
String replace = getData.replace("u0026", "&");
myTv.setText(replace);
}
Now on my onCreate Method
TextView tv = (TextView) findViewbyId(R.id.my_textview_result);
tv.setText(resultFromWebsite);
symbolTextFilter(tv);
It will not make that correction. It will if I put the symbolTextFilter(tv) in a onClickListener button though. I don't want to assign the correction in a button. I want it automatically. My guess is, everything that I have in the onCreate is happening too fast for corrections to be made. How do I fix that? Thanks in advance!
Try this:
tv.setText(symbolTextFilter(resultFromWebsite))
You should use the method symbolTextFilter to handle the string only:
public void symbolTextFilter(String input) {
if (input.contains("u0026") {
return input.replace("u0026", "&");
} else {
return input
}
Nevermind, I got it! I'm not sure where the "\" came from because it wasn't in the original string that it pulled before the correction. I fixed it with this...
public String symbolTextFilter(String input) {
if (input.contains("u0026") {
return input.replace("\\" + "u0026", "&");
} else {
return input
}
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+"");
Can anyone tell me what's wrong with the following listener? I'm always crashing...
#Override
public void onClick(View view) {
Editable num1 = NumberOne.getText();
Editable num2 = NumberTwo.getText();
int um1 = Integer.parseInt(num1.toString());
int um2 = Integer.parseInt(num2.toString());
Results.setText(um1 + um2);
}
The setText method accepts a String as an argument. The result of um1 + um2 is going to be integer. I'd suggest you first convert the result to a String and then set it inside the setText method.
Something like this should work:
Results.setText(Integer.toString(um1+um2));
Even better you can do this:
Results.setText(Integer.toString(Integer.parseInt(num1.toString()) + Integer.parseInt(num2.toString())));
The reason that it is not working is because you cannot set text to an. You have to use Results.setText(String.valueOf(um1 + um2)) (as Andre stated) to convert the integers to strings. Therefore, the setText will work.
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 have a method that checks for a null value from an editText on a click of a button like so:
public void myClickHandler09(View chv){
if (text9.equals("")){
text9.setText("0");
}else{
converter(text9);
}}
The
converter(text9);
method is as shown:
public void converter(View view){
switch (view.getId()) {
case R.id.Button09:
RadioButton RadioButtons = (RadioButton) findViewById (R.id.RadioButton901);
float inputValue = Float.parseFloat(text9.getText().toString());
if (RadioButtons.isChecked()) {
text9.setText(String
.valueOf(convertRadioButtons(inputValue)));
}
break;
}}
private double convertRadiobuttons(float inputValue){
return inputValue * 6.4516;
}
The method is larger but here i've only called one radiobutton to shorten it.
Right now though the if statement seems to do absolutely nothing and so non of the rest of the code works. If i remove the method and rename
converter(View view){
to
myClickHandler09(View view){
then the code works and until you enter a null value into the EditText (then it crashes)
What am I doing wrong exactly here?
NOTE: the method name "myClickHandler09" is linked to the button as android:onClick in the xml
You need to do if("".equals(text9.getText().toString())) { ...
The toString() is there because the TextView will return a CharSequence which may or may not be a String.
Right now you are comparing the TextView itself to "", and not the String it is showing.
Edit - As far as the crash goes, you also want to catch the NumberFormatException that Float.parseFloat() throws.
float inputValue = 1.0f; // some default value, in case the user input is bad.
try {
inputValue = Float.parseFloat(text9.getText().toString());
} catch (NumberFormatException e) {
// possibly display a red flag next to the field
}
Why not try
if ("".equals(text9.getText())) {
} else {
}
You essentially have to do a getText() from a TextView and not equals a String with a TextView.
One thing I don't understand with your code is that you call:
converter(text9);
passing in the EditText, but by replacing converter(View view) with the function name myClickHandler09 (like so):
myClickHandler09(View view) {
the button being pressed with call this function (if you defined it in the xml layout onClick paramter).
So to match this behaviour with your current code, try this out:
public void myClickHandler09(View btnView){
if (text9.equals("")){
text9.setText("0");
} else {
converter(btnView);
}
}
I may have missed the point of you're post, but I think that is part of your issue. Also in stead of .equals("") I prefer (text9.toString().length() > 0) just seems a bit more logical, but that's me being a bit pedantic.