Notify a variable Android - android

Please excuse me if I am being a noob, but I just completed an Android programming tutorial, and tried making my own app, but don't know how to do something (which is ok, right?). So yeah, I made a simple calculator app, and tried to have it notify the user the result, so I made an int called result which is the value of two editTexts added together. I tried making a notification with the contentText being the result int, but I don't reallyknow how to do that as it will only take a string... Help. If you need the code the just say so.

You can convert the int to a String using Strings valueOf method:
String string = String.valueOf(result);
Or you could use String format method if you wish to have text too:
int result = 0;
String.format("Result: %d", result);

Try converting int to String like this
String resultString = String.valueOf(resultInt);

Related

String to text field in android app

I have the following data scanned from a pdf417 and need to extract certain text to certain text fields (already created), not sure how to go about this... Data scanned with manatee works plugin and android app using android studio.
All help will be appreciated.
Data that was returned from scan -
%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%
Each part between the %'s need to go to a text field. I know that I need to make use of String substr=mysourcestring.substring(startIndex,endIndex); but this will work up to the first 2 % signs. How do I continue to the next few?
Thanks.
If you want to split string based on a delimiter, use the following
String delimitter="%";
String[] parts = inputString.split(delimitter);
Why not use String.split()?
In your case it would look something like this:
String[] extractedStrings = mysourcestring.split("%");
You can work on your string by using split method:
String yourString = "%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%";
String[] split = yourString.split("%");
In this way you will get an array where each item is a substring between two % chars.

Minusing string number values in android

I am making an android application where i got a set of strings that i load from SharedPreferences so that i can save the strings. The strings contain only numbers, but it is not an int value, its a string value. And i wounder how i can minus the numbers that's in there, becuase usually, i would have been using something like an int value = value - value; But that doesn't seem to work since it's a string and not an int value. How can i do this even though it's a string? I know i could use int values instead, but as i didn't think of this before now, when i'm almost done, it would be alot of work changing all of the code that's related to this. Please help me and thanks so much in advance!
You will have to convert your strings to ints first, then operate on them, then save the string back:
String value = preferences.getString("key:");
int intValue = Integer.valueOf(value);
intValue = intValue - 1;
preferences.edit().putString("key", Integer.toString(intValue)).commit();
Try using Integer.valueOf(string) or Integer.parseInt(string).
Learning basic programming the the concept of casting will help you tremendously. One datatype can be converted to another using the base classes, which often times deal with String. For instance look at the Documentation of Integer.
Well as you have said that it is a lot of work to change the code and save it as int, I would suggest converting the string into an integer, refer to this link for more information, someone has asked about converting strings to integers, and as Android is Java-based, this can apply to your project:
Converting a string to an integer on Android
Hope this helps.

Android math operator

I need calculate a thing. but my formula sentence has occur some problem.
TextView ticketP = (TextView)findViewById (R.id.ticketQ);
ticketP.setText(oneSession.getTicketOder());
String Ctotal = "";
Ctotal = jsonObject.optString("price");
String OneTotal = oneSession.getTicketOder() * Ctotal; // this part has occur the problem which is the operator * .
You'll need to convert the Strings to numeric type before performing any multiplication. Depending on the type of numeric value you are using take a look Double.parseDouble(String string) or Integer.parseInt(String string).
int oneTotal = Integer.valueOf(oneSession.getTicketOder()) * Integer.valueOf(Ctotal);
to convert it again to String use
String.valueOf(oneTotal)
Yes. Your going to have to use the parsing methods in order to convert the string to a native numerical type. You also need to be care about a few things with your code.
json.optString() can return null. opt = optional.
I would suggest using json.getInt() or json.getDouble() this will not only give you the correct type, but also throw an exception if the values aren't correct.
Secondly your going to have to convert your numerical answer back to a string if you want to display it. But this is easy enough with a .toString() or + "" if you are lazy.

Why does this trigger a force close on Android?

Why does this code trigger a force close in Android?
`score.setText(Integer.parseInt((String) score.getText())+1);`
score is a TextView, and I am simply increasing the number by 1. I have predefined a String resource to be the initial number in the score TextView.
I am quite frustrated.
First off you should try breaking down your code so you can actually see what is going on with it.
Instead of
score.setText(Integer.parseInt((String) score.getText())+1);
try
String tmp = score.getText().toString();
int score;
score = Integer.parseInt(tmp) + 1;
score.setText(String.valueOf(score));
EDIT: Upon further reading of the documentation, setText has several overloads, one of which DOES take an int, but it takes the int of a resource ID. My guess is that your score is not a valid resource ID, thus crashing your application.
public final void setText (int resid)
Oh and as far as the frequent FC's when beginning Android Dev, it happens to the best of us. The key is to learn WHY the FC's happen, and have a LOT of patience.
mostly u need to do this
score.setText(Integer.parseInt(score.getText().toString())+1);
coz.. getText() returns a Editable Object which cannot be parsed to Integer. So it give NumberFormat Exception.
AndMake sure to set TextView,s Text to an integer initially..
try this way
score.setText(String.valueOf(Integer.parseInt(score.getText().toString())+1));
as you can pass the integer value that's why getting force the application
TextEdit.setText takes a CharSequence as input.
You are supplying an integer through Integer.parseInt((String) score.getText())+1
See, if converting it back to string and using it in setText helps.
You can convert an integer to string using Integer.toString.
PS: I am new to java myself.
The compiler should have ideally caught this error.
It's possible java uses some implicit type conversions from string to int.

EditBoxPreference() storing the value as an int instead a string

I'm writing an Android application that can easily back up files and folders to the users PC. One of the things I wanted to implement was allowing the client running on the Android device to change the port I will be sending the file to.
For this, I've created an EditTextPreference to store the value.
The code I'm using to get this value back is
port = prefs.getString("serverPort", "<unset>");
However, this returns a string and I need an int, so I tried to use
sendPort = Integer.parseInt(port);
But this crashes the Android application, with (I think) a number format exception.
Is there anyway I can explicitly store the value that is entered as an Integer to make it easier?
I tried to use the method
port = prefs.getInt(...);
but that didn't work either.
Thanks for any help.
This will take whatever is entered into your edit text and put it in an int.
int yourValue = Integer.valueOf(editText.getText().toString());
Note that Integer.valueOf() will return a format exception if you put a String in it that doesn't have an integer value.
You can then use
prefsEdit.putInt("serverPort", yourValue);
prefsEdit.commit();
to save it to preferences. And this to retrieve it
int port = prefs.getInt("serverPort", -1);
Saving the port as String or int doesn't make a big difference. Either way you'll have to convert it.
Your app crashes, because it cannot convert <unset> to a number, that's where the NumberFormatException comes from.
Solution:
Catch the NumberFormatException and set sendPort to a default value.
port = prefs.getString("serverPort", "<unset>");
try {
sendPort = Integer.parseInt(port);
} catch (NumberFormatException ex) {
sendPort = 1234;
}

Categories

Resources