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
Related
the problem is that I have various XML files with the data that I need for my app and I want to access to read them by the name of the XML file: "fileName.xml" so I can run the pull parser. Since the program only knows which file to load by its name which is given by a string I can't call them using r.id.fileName.xml.
I have tried things like:
int resID = getResources().getIdentifier("r.id.fileName", "id", getPackageName());
and then call it by this resID... but it doesn't seem to work (resID is always 0) :(
Any ideas?
Thank you!
The best solution from a performance standpoint is to have a mapping in your Java code between some string and the resource ID value:
private static HashMap<String, Integer> RESOURCES=new HashMap<>();
static {
RESOURCES.put("thingy", R.xml.thingy);
RESOURCES.put("thingy2", R.xml.thingy2);
// and so on
}
Then, you can just call RESOURCES.get() to retrieve the ID value, without having to use the reflection that goes on inside of getIdentifier().
If you really want to use getIdentifier(), then the syntax would be:
int resID = getResources().getIdentifier("fileName", "xml", getPackageName());
because:
the first parameter is the base name of the resource
the second parameter is the type of the resource, and the files in res/xml/ are not id resources
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
I want to get a string resource name or ID by passing its value.
Example :
<string name="stringName">stringValue</string>
I want to pass the stringValue and get the stringName or ID (id value)
Based on ID i will do some calculation to get another ID of another String resource
I don't know if that's possible and I think the IDs can change without warning if the R class gets newly generated. You could of course try some reflection magic on this class, but I would recommend against it.
I also have this problem, and can think of 2 possible workarounds:
load all the strings and their names into a table, and look in the
table.
Or cycle through my complete list of names, getting the string resource
for each one, and comparing it to my known string resource.
I am implementing the 2nd one, as my list of string resources is not very big, and I don't have to do this operation very often. Once the name is known, it's possible to get the Resource Id via:
//Get resource id from name
var resourceId = (int) typeof (MyApp_droid.Resource.String).GetField(MyStringName).GetValue(null);
(code is C# because I'm working in Xamarin).
In Android, to access a string from the strings.xml file, we use R.string.string_id. Would it be possible to have a method such that we'll use the string form of string_id? I mean can we for example have a method GetString("string_id") to retrieve R.string.string_id?
//Replace this with appropriate context
String name = "name_of_your_string_in_strings_xml_file_goes_here";
int resId = this.getResources().getIdentifier(name, "string", this.getPackageName());
String string = this.getResources().getString(resId);
It is possible. You have to understand the fact that any data that is pinned to xml files in android are considered as resource to the app. And android resource can be accessed only by means of unique id we provide to them. These ids that are generated are of type int and hence we need to pass a int parameter to get a reference to these resource at run time.
But it is not that only the default way to access them is by using their res value but instead if you could know the name of the string key you could find the id from there and then use the int id to do this.
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