How Do I Update TextView Result On App Launch - java

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
}

Related

Does there have to be a string value in my setText?

Im trying to make an app that converts the value that the user inputs into the editText. But when I run the program it shuts down and won't show the input the user inputted or the input divided by 2. I tried putting btCalculate.setText("Hi" + convert); and that displayed hi and the user input but when I get rid of the string and just have convert it shuts down. Can someone help me or make sense of what I'm trying to do?
Here is my code:
btCalculate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int convert = Integer.parseInt(editText.getText().toString());
if (btCopper1.isPressed());
btCopper2.isPressed();{
btCalculate.setText(convert);
}
if (btCopper1.isPressed());
btSilver2.isPressed();
btCalculate.setText(convert/2);
}
});
}
}
Is a good practice to code as simple as possible. It will be easier to found and fix issues.
Exemple:
if(expression == true) {
// do this instructions
}
In your code, start fixing the {}, making it easier to understand:
if (btCopper1.isPressed()) {
btCalculate.setText(convert);
}
Think about it, it seems the error is in your logic.

Failed to get string from autocompletebubbletext

i'm using autocompletebubbletext library (https://github.com/FrederickRider/AutoCompleteBubbleText) which display the list of items to chose from in a list and allow in same time to chose the items from the editetxt..
My problem is as follow:
after the user choses a number of items(=Multiple inputs) .. i want to display a text as an output when clicked on a button (depending on the items chosen of course) as explained in this picture: (https://i.imgur.com/QQuzFvl.png)..
but i got stucked in getting the string of itemsChosen from the edittext
FIRST: i am not sure which return value to use!!
SECOND: i assumed i should use "checkedIds" and I've tried A lot of solution in internet , i've been trying different ideas all day, from what i have tried: ( Ps: i used a toast to see if the methods did work)
edittext.getText().toString() > nothing appears in Toast
i have tried to turn the setHash to String[]: then turning the String[] to one string like:
content=editText.getCheckeditems();//getcheckeditems returns checkedIds which is = new HashSet<String>()
String[] BLANA= content.toArray(new String[content.size()])
data= TextUtils.join(",",BLANA);
it didnt work, in Toast i got"[]"
For the MainActivity.Java (i have the same as here):
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/samplelist/src/main/java/com/mycardboarddreams/autocompletebubbletext/samplelist/SampleActivity.java
For MultiSelectEditText.java (i Have same as here) :
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/library/src/main/java/com/mycardboarddreams/autocompletebubbletext/MultiSelectEditText.java
WHAT is the solution? (to get a string so i can use it later)
PS: if there is another way(another library or methode) to get what i want to achieve in the first place , i would love to try it..
EDIT: THIS IS A CODE THAT LOOKS PROMISING BUT DIDN'T WORK!
in MultiSelectEditText.java
public String datachosen(){
String [] blan= checkedIds.toArray(new String[0]);
StringBuilder builder = new StringBuilder();
for (String string : blan) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String DATATORETURN = builder.toString();
return DATATORETURN;
}
in MAINACTIVTY.JAVA
MultiSelectEditText editText = (MultiSelectEditText)findViewById(R.id.auto_text_complete);
content=editText.datachosen();
Toast.makeText(DecisionTree.this, content,
Toast.LENGTH_LONG).show(); // TOAST INCLUDED IN A BUTTON OF COURSE
OUTPUT: TOAST SHOWS NOTHING!
Solved it ..
i intialize the edit text before on create ..and defin it later after onCreate()..
and got string with the normal edittext.getText().toString(); method!
Simple but was hard to detect the problem!

Parsing issue android widget to int

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())

Android - The method setText() is changing text of a widget even though it is never called

I have a basic block of code here with simple data passing between activities. Basically when there is data received, change the text of the button:
Bundle intentData = getIntent().getExtras();
if (intentData != null) {
String passedMsg = intentData.getString("userMsg");
Button mainButton = (Button) findViewById(R.id.main_button);
mainButton.setText(passedMsg);
}
However, even on cases when the if conditional fails, the text of the button still changes. When I comment out the line mainButton.setText(passedMsg);, the text of the button remains unchanged.
It seems as though the presence of setText() alters the button's text regardless of whether that line of code is reached. Why does it do this?
Obviously, your block of code is being executed multiple times. to prove this, do something like
static boolean initialized = false;
public void enteredBlockOfCode() {
if(!initialized) {
// code here only runs once ...
initialized = true;
}
}
Either the condition you're checking is not the one you should be checking, or in the code you've compiled the if statement is immediately followed by a semicolon before the braces.
It seems that the intentData isn't ever passed null and so the if statements don't fail

if statement problems in android

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.

Categories

Resources