Dynamically adding an int to a textView - android

This may seem like a stupid question, but for some reason i just can't figure it out- i have a few textViews i add text from an Object all the string fields are added fine. but when i try to add a int to the textView it crashes my application.
heres my code
firstLineOfAddress.setText(addline);
town.setText(town2);
county.setText(county2);
postCode.setText(post);
// the three strings up above work fine if i comment the three below out
// ask , current, done are all ints
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);

set like this:
textView.setText(10+"");
In your code replace this:
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
with
askingPrice.setText(ask+"");
currentOffer.setText(current+"");
doneUpValue.setText(done+"");

The problem is that you have to convert your int to String. To convert your int to String, use :
Integer.toString(myInt)
or
String.valueOf(myint)
So it would be askingPrice.setText(Integer.toString(myInt))

when you add any Integer number the call this...
textview.setText(""+value);
OR
textview.setText(String.valueOf(value));
where value is a Integer.

You have to set the text value as a String. Convert it as so:
askingPrice.setText(String.valueOf(ask));
currentOffer.setText(String.valueOf(current));
doneUpValue.setText(String.valueOf(done));

try to convert it to a String perhaps .
askingPrice.setText(String.format("%d",ask));

well trying above scenario like this
int text=0;
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(text);
throws exception as
(26955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.highactivity/com.example.highactivity.MainActivity}: android.content.res.Resources NotFoundException: String resource ID #0x0
because while passing integer in set text it takes as resource id.At run time search for resource id =0 but there wont be any resource id like that so it throws exception will leads to app crashes.convert int to string if you need to display integer

Related

Two different return types in if else statement in data binding

I am trying to set text in TextView using data binding. I am using if else statement and if the value is true I want to set String to that TextView and in the other case I want to assign id of the String resource. My Code:
android:text="#{object.isTrue ? object.getString : object.getStringId}"
But when I try to do it I get error that Integer cannot be converted to String.
Everything is alright when I try to assign this String resource it directly like this:
android:text="#{object.getStringId}"
Is it somehow possible to use in that if else statement two different return types?
Yes, you can use Context.getString() on the second one, so that both are a String. I think this should work:
android:text="#{object.isTrue ? object.getString : context.getString(object.getStringId)}"
You don't have to import context, it's auto imported.

how I can get array based on another array position ? in android

I want to get an array based on another array click position like :
String[] nissanArray = getResources().getStringArray(R.array.nissan);
String name = nissanArray[pos];
int nameInt = Integer.parseInt("R.array." + name);
targetArray = getResources().getStringArray(nameInt);
and I got an error when using this : I believe the error starts from Line int ....
You can use the getIdentifier method of class android.content.res.Resources. See the documentation for the details.
It allows you to convert a resource name to the integer resource id.
do you know what you are trying to do ?? first understand your code
getStringArray(R.array.nissan);
in that integer parameter we should pass
that you are trying to pass integer using convert string into int is totally wrong
it required integer which mentioned in R.java file and it cant retrieved it by your syntax
when you write (R.Array.nissan)
then it linked R.java file and return integer for that from file

showing a integer number in edit text field in android

I am trying to do the following :
t1 is an edit text
a and b are two integers
t1.setText(a+b);
but this is not working in android but it works perfectly with javaswing
A simple solution would be:
t1.setText("" + (a+b));
or maybe:
t1.setText((a+b).ToString());
please do like the following
EditText et=(EditText)findViewById("ID of your EDITTEXT");
int c=a+b;
String s=c.toString();
et.setText(s);
This'll work fine :) If it works vote me :)
but this is not working in android but it works perfactly with
javaswing
It will not, because you're actually calling setText(int resId). See here. Calling this method will search a string in xml resources (E.g. strings.xml). Every id of a string (E.g. #strings/hello) will be compared to resId, If resId matches the string's id that string will be displayed to that widget or else you wil get a ResourceNotFoundException
To display the actual integer, convert it first to String
t1.setText(String.valueOf(a+b))
You are doing like this t1.setText(a+b); it search this id (a+b) in resource file like strings.xml that is not available. So it will throws a exception ResourceNotFoundException..
So to set the number in text view you need to convert it into string.
In Android you need to do like this:-
int sum = a + b;
String sumString = String.valueOf(sum);
t1.setText(sumString);
OR
t1.setText((a+b) + "");

How to access values from "strings.xml" dynamically in android?

App version 2.3.3
Here is what i am looking for...
I have few strings in "values/strings.xml". I wish to retrieve the values depending on the string's "name" attribute and use it my application. The input to which string in the xml should be used comes dynamically. Here is an example...
My input from other file :: "A" => This value changes. It can be any value in A,B,C,D.
I have different strings in strings.xml, like...
<string name="A">AforApple</string>
<string name="B">BforBall</string>
<string name="C">CforCat</string>
<string name="D">DforDog</string>
Now, how do i programmatically get the value(AforApple) of the String with name="A".
String str = context.getResources().getString(R.string.A)
or u can use
textBox.setText(R.string.A);
do not forget to import the package com.yourpackackage.R.
You need to use getResources() method:
String a = getResources().getString(R.string.A);
Just for the record, you can also dynamically generate it using reflection.
int stringRes=R.string.class.getField("Here you put the dynamically generated input, such as A").getInt(null);
textView.setText(stringRes);
This will return the resource int value from string XML based on the input, as long as the input value "A" matches string name in the XML, this will retrieve them dynamically.
To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:
String A = getString(R.string.a_string);
If your code gets a string like "A" and you are trying to dynamically find the string in your resources that matches that name, I don't think you can do that.
Instead of using the strings.xml, you might want to use arrays.xml and build a HashMap from that before you need to access those strings
Try this:
String word = getResources().getString(R.string.A);
Check out the link here.
You can use this code:
getText(R.string.A);
Basically, you need to pass the resource id as a parameter to the getText() method.

Using Reflection with Android

I faced an interesting problem today. I have 4 strings which I need to show on app on random basis. So I simply added the strings to my string.xml and was setting my textview to show the text as
textView.setText(R.string.text_1);
or (if random number was 2)
textView.setText(R.string.text_2);
and so on
I observed that the change is just in last character, so tried using reflection
Class c = Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("text_"+num); //num is 1/2/3/4
System.out.println("********** "+field.get(null));
Now the field.get(null) actually return the Id (hexadecimal number in R.java) value instead of string value I would expect.
Is there a way to fetch actual value of string using reflection or this is something in android which I will have to live with?
getResources.getString(resourceId);
R.string.text_2
will always return the hex number. To really get the string value, you have to try the following
MyActivity.this.getResources.getString(R.string.text_2);
You can simply request your resource ID from the resource manager:
Class c = Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("text_" + num);
int resId = (int)field.get(null);
String text = this.getResources().getString(resId);
textView.setText(text);
I suggest you to use array of strings and choose random item from it

Categories

Resources