I know you can set the int id of a View using the following:
View.setId(int id);
But is there a way to use a string instead? I know what I could do is declare an array of arrays and each array contains a string and an int associated with that string and if the string is called then it gets the int associated with that string, but is there a simpler way to do it to set the id a as a string?
Also, when a View is created during runtime on the app, does it automatically generate a unique int id for it? Thanks for the help.
but is there a simpler way to do it to set the id a as a string?
No, there is no. View has just the setId(int) method. If your string is a number, you can convert this to int and use as id for your view.
Also, when a View is created during runtime on the app, does it
automatically generate a unique int id for it?
no you have to take care of it.
Related
I'm trying to change name of the post param dynamically.
#FormUrlEncoded
#POST("/payment/send_rc")
Abuse setTop(#Field("MrchLogin") String login,
#Field("OutSum") int sum,
#Field("InvId") int invId,
#Field("Desc") String paymentDescription,
#Field("shp_payment_no") int adtId,
#Field("shp_type") int number,
#Field("shp_user") int userID,
#Field("shp_month[]") int monthPeriod);
This is my method that I'm using and I'm trying to set monthPeriod field with dynamic name.
So it can be something like shp_month[n] where n is my custom integer param name. So is this possible to change name of the post field dynamically?
As Jake Wharton said
You can use a "#FieldMap Map<String, String>" for that.
Looks like Robospice Retrofit module is out of date.
I'm building a Class to populate my layouts using a JSON Schema. Basically in JSON I have a definition { type: "checkbox", label: "Label text", value: true, id: "liquids" }. The problem is I don't just have to render the UI i also need to collect values later when the form is posted to submit it to my API service.
The ID of each element is a string when I create the View element on my layout I need to give it this id however View has setId() method however this can only be an integer also has to be unique.
Is there any way I can use the string as id ?
You can use Strings as a form of ids by passing them to your View's setTag() method.
Then, to find a particular View, use findViewWithTag()
Just make sure that your String ids are unique.
you could use the hash code of the string as the id.
view.setId(string.hashCode());
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.
Is possible in Android to findView by String Id?
I add some rows in table programmatically and in next iteration need to remove some of them and I have List id ( "tblRow_1", "tblRow_3" ..}). Can I retrieve by ids from the list?
Use getResources().getIdentifier() to retrieve the actual integer resource ID.
example
int resID = getResources().getIdentifier(stringVar, "id", "com.sample.project");
view = findViewById(resID);
You can use Resources.getIdentifier() for this.
I have some messages being passed back from my server through php. The problem is that the messages are in English and if the user is using another language they will still get the message in English.
So I had an idea that maybe instead of passing back the message I would instead pass the String resource Id from the android app, that way the app will get the correct string id for their language. I will use this in a number of apps so I just want to know if the string id is guaranteed to be the same across different android projects?
No.
The string resource IDs are likely not even guaranteed to be the same between re-compilations of the same application (e.g. differences between versions of aapt).
Christopher is right, but you can pass a parameter to your server like http://example.com/script.php?lang=en or lang=fr ....
This way the php scripts can use this parameter to return messages in the relevant locale.
You can get the resource ID from a string:
int resID = getResources().getIdentifier("org.my.package:strings/my_string", null, null);
// or
int resID = getResources().getIdentifier("my_string", "strings", "org.my.package");