This question already has answers here:
How to get a resource id with a known resource name?
(10 answers)
Closed 8 years ago.
Lets say i have a few views and I want to get the id name of their IDs
view.getId()
returns an int value but instead i want to get the name of the id
android:id="#+id/valueFrance"
So is there any method on java that can return the name/value of the id ( "valueFrance" ) ?
Thank you very much.
try this
getResources().getResourceEntryName(int resid);
Related
This question already has answers here:
Save ArrayList to SharedPreferences
(38 answers)
Closed 6 years ago.
I have an array integer in android studio like this { 0 , 0 , 0 , 1 , 1 , 0 , 1}
how can I store and recall it in shared preferences
There is no way you can store an array as it is.
Either you can convert it to JSON String and store it as a string.
Or you can store each element of the array individually with an ID like this.
Build a string out of it. Preferably using StringBuilder.
And, when reading, just split() on commas and then user Integer.parseInt() on all elements of the array.
Should be faster than the json route and does not require any extra dependency.
This question already has answers here:
String comparison - Android [duplicate]
(17 answers)
Closed 6 years ago.
In part of my application I get date from mobile and store it in SQLite Database as string.
when I try to select rows from database which have same date, can't find any match result.
Also as in below code, I tried to get specific cell of table which I'M sure it's match the current date. but no match.
is there any idea?
Get Date from mobile and insert it as string.
String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
MyDB.insertRow(currentDateString);
Check if date from database equal mobile date.
if (MyDB.getRow(21).getString(1).toString()==currentDateString)
Toast.makeText(getBaseContext(),"Match",Toast.LENGTH_SHORT).show();
You don't match Strings in Java using == operator. You use .equals() method as String is not a primitive type.
So try this -
if (MyDB.getRow(21).getString(1).toString().equals(currentDateString))
Strings aren't matched by == operator because it is an object.
try
MyDB.getRow(21).getString(1).toString().equals(currentDateString)
and if you need it not case sensitive
MyDB.getRow(21).getString(1).toString().equalsIgnoreCase(currentDateString)
Also
make sure that the inserted date format is the same that you match with
This question already has answers here:
Access resource string by string name in array
(2 answers)
Closed 7 years ago.
Think about a list of parameters and their respective intervals of discrete integer values:
a[1-N], b[1-M], c[1-K], d[1-J]
a, b, c, d are the variables while between square brackets there are intervals of their possible values.
At runtime if they are
a=1, b=2, c=3, d=5
then I'd like to get the resource with
name = R.string.string_1_2_3_5
Is it possible?
I wouldn't want to make a series of cascade switches for each variable to finally pick a resource. I know this could work but is there another way?
You can use Java reflection like in here.
If you need to retrieve strings like that more than once, in order to get fast access, you should first construct a hashtable with the field names of R.string as keys (maybe when you launch the app).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can i get the resource id of an image if I know its name?
hi i want to get the id of an image,i could do this,
int id = R.layout.imagename
then i could do this
imageView.setImageResource(id)
but my question is how to get the id if i know the imagename changes at run time, so on this case how can i make R.layout."imagename" ?
I just copied Francesco's answer, I hope you will up vote his answer
String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
This question already has answers here:
Difference between getString() and getResources.getString()
(3 answers)
Closed 9 years ago.
I read about String Resources and I understood that you simply use the getString(...) method in order to read the value of a string from res/values/string.xml. Then I read that you can also use getResources().getString(...).
What is the difference between these two ways of obtaining the value of a string?
No any difference. They're both equal.