Normally, we use R.String.btnClose to get a ID.
Sometings, I hope to use the following code to ID, I know the code is wrong.
I don't know if java support macro var, if so, how can I write code? Thanks!
String s="btnClose"
R.string.s
You may want to try Resources.getIdentifier.
An equivalent for
int id = R.string.btnClose;
would be
int id = getResources().getIdentifier("btnClose", "string", getPackageName());
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
For example you could have a Map<String, Integer> which returns your id from name.
Inside of activity use this :
String s = getString(R.string.btnClose );
Not in Activity :
public String getText(Context context, int resourceId)
{
Resources resources = context.getResources();
return resources.getString(resourceId);
}
Related
Can I get this string in code?
GetId() returns an int?
EDIT:
If not, is there a way I can assign my textboxes a tag each, which is a string, and then return this in code?
Can I get this string in code?
No. You can get only ID of resource. Reason is because your R.class storing all resources as
public static final int fields
Your R.java can looks similar like this:
public static final class id {
public static final int DataHallTextField=0x7f09000a;
}
Update:
If not, is there a way I can assign my textboxes a tag each, which is
a string, and then return this in code?
So i think you are looking for this:
String name = getResources().getResourceEntryName(R.id.DataHallTextField);
If you want also package you can use
String name = getResources().getResourceName(R.id.DataHallTextField);
You are looking for the resource name:
getResources().getResourceEntryName(intresid);
If you want the resource name include package name then use:
getResources().getResourceName(intresid);
i have string like #string/screen_enable_auto_mode. I know that i can use:
String mess = getResources().getString(R.string.screen_enable_auto_mode);
and i will get a message.
But i have whole string as #string/screen_enable_auto_mode. How can i parse it to
String mess = getResources().getString(R.string.screen_enable_auto_mode);
that i will get a message from xml?
You can use Resources.getIdentifier for this.
int resId = getResources().getIdentifier(
"screen_enable_auto_mode", "string", "com.package.app");
String mess = getResources().getString(resId);
Replace "com.package.app" with the actual package of your app, of course.
I'm using the below code to find a resource by id;
setContentView(R.layout.golders);
for (int i=1; i<hm.size()+1;i++)
{
int id = getStringIdentifier("Bus"+i);
view = (TextView)findViewById(id);
view.setText(hm.get(i).toString());
}
My getStringIdentifier() is working but when I try to set the text I'm getting a NullPointerException.
I've used the setContentView to focus on the golders.xml file which has the ids that I want to update.
I've tried Cleaning the project but that hasn't done anything either, any ideas?! Thanks!
EDIT:
public int getStringIdentifier(String aString)
{
String packageName = "com.example.bustimetable.Robbos";
int resId = getResources().getIdentifier(aString, "string", packageName);
return resId;
}
Your getStringIdentifier(String) method returns a string ID (something from R.string). You need a new method, something like getIdentifier(String), that will return soemthing from R.id. I can't see the XML, so I don't know what your TextView's ID is, but... you'll want to verify that the ID is, in fact, Bus_ where the _ is some number.
public int getIdentifier(String aString)
{
String packageName = "com.example.bustimetable.Robbos";
int resId = getResources().getIdentifier(aString, "id", packageName); // Get from R.id, not R.string
return resId;
}
The problem is that getStringIdentifier() is returning an id for a string resource. Apparently the id which is returned is not a valid id for a view resource. Even if was, I don't know how you can guarantee that you will get the view that you want. You need to either add another method to return id's for view resources, or modify the one you have so that it will return id's for either string or view resources
I have a method that returns one of about 20 possible strings from an EditText. Each of these strings has a corresponding response to be printed in a TextView from strings.xml. Is there a way to call a string from strings.xml using something like context.getResources().getString(R.strings."stringFromMethod")? Is there another way to call a string from a large list like that?
The only methods I can think of is converting each string to an int, and use that to find a string in a string array, or a switch statement. Both of which involve a huge amount if-else if statements to convert the string to an int, and would take just enough steps to change if any strings were added or taken away that I'd be more likely to miss one and have fun bug hunting. Any ideas to do this cleanly?
Edit: Forgot to add, another method I tried was using was to get the resourceID from
int ID = context.getResources().getIdentifier("stringFromMethod", "String", context.getPackageName())
and taking that integer and putting it in
context.getResources().getString(ID)
That doesn't appear to be working either.
No, you can't. The getString() requires the resource id in integer format, so you can't append a string to it.
You can, however, try this:
String packageName = context.getPackageName();
int resId = context.getResources().getIdentifier("stringFromMethod", "string", packageName);
if (resId == 0) {
throw new IllegalException("Unknown string resource!"; // can't find the string resource!
}
string stringVal = context.getString(resId);
The above statements will return string value of resource R.string.stringFromMethod.
You need to use reflection (pretty ugly but only solution) load the R class, and get the relevant field by you string and get the value of it.
this is what I used to do in these kind of situations, I will made a Array like
int[] stringIds = { R.string.firstCase,
R.string.secondCase, R.string.thridCase,
R.string.fourthCase,... };
int caseFromServer=getCaseofServerResponse();
here caseFromServer varies from 0 to wahtever
and then simply
context.getResources().getString(stringIds[caseFromServer]);
I would like to ask how can I get the value of predefined value to its name
The following is my code
public class Calculation_Activity extends Activity{
int a=1;
int b=2;
int c=50;
int result;
String array1[]=new String[]{"a","b","c"};
}
I would like to ask how can I get the value of the string by using array1[i]?
for instance, I would like to use array1[3]to call the value of c[ie.50]
May you give me some advice on this matter?
You might solve your issue by using a Map and its standard implementation HashMap:
Map<String, Integer> values = new HashMap<String, Integer>();
values.put("a",1);
values.put("b",2);
values.put("c",50);
String array1[] = new String[] {"a","b","c"};
int result = values.get(array1[2]); //result = 50
// or
int result = values.get("c"); //result = 50
You can use a HashMap (http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html), is a dictionary like data structure where you can store key-pair values
What you're trying to achieve would be better suited to a dynamic/scripting language. Have you considered using a Map instead of multiple varaibles?
In Java, this is not a common approach, such as it would be in scripting languages. You could try to use a Map (ie HashMap), which would enable you to achieve what you want, sort of.
In fact I think it is possible to do exactly what you want using reflection in Java, but I would not go there!