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.
Related
I have a question. I know I can store an image as byte array in SQLite database. But this needs some extra CPU work, so I tried to store only the id (R.drawable.myImage).
I stored it as an integer and I can retrieve it back. I am setting my image into imageview like this:
ivSportIcon.setImageResource(getItem(i).getIcon());
But I get an error:
Resources$NotFoundException: Resource ID #0x8
Is this a wrong way? Should I store my image as a byte array? Thank you in advance!
I do not know how you are storing the resource ID, but 0x8 is not a resource ID.
Beyond that, bear in mind that resource IDs change with each build, and so the resource ID that you save today will be different tomorrow.
Instead, store some other identifier in the database, that you can use to decide which of your drawables to use.
The only reason to store the actual image data in the database would be if you plan to change the images in the future and you want the user to be able to use the old images that happen to be associated with database entries.
You could store the name of the Drawable as a String instead, and use this method to retrieve its id:
public static int getResourceIdForName(Context context, String drawableName) {
final Resources res = context.getResources();
final String packageName = context.getPackageName();
return res.getIdentifier(drawableName , "drawable", packageName);
}
This is a little inflexible however, as if you decide to change the name of your drawable in future, then you will also need to change the name in the database.
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.
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());
In an Android app I have various TextView instances whose IDs are something like: android:id="#+id/a_key where a_key is a word with no numerical values.
A Java class (not activity) parses an external XML file and populates a Map like this:
HashMap<String, String> map = new HashMap<String, String>;
map.put(a_key, a_value);
where the String a_key contains the exact same word of android:id="#id/a_key".
How would I go on about assigning a_value to the android:text resource whose ID is represented by the String a_key?
I apologize if this is confusing. I can post the code if it helps, but I tried to keep it as generic as possible.
Thanks in advance :)
A better way to do this would be with the way codeMagic suggested.
Using
int id = getResources().getIdentifier("a_key", "id", "com.example.app");
Then of course using
Textview a_textView = findViewById(id);
You will need to iterate through the entry set of the map to do it this way so use
map.entrySet()
ex:
for(Entry<String, String> entry : map.entrySet())
{
int id = getResources().getIdentifier(entry.getKey(), "id", "com.example.app");
TextView textView = findViewById(id);
textView.setText(entry.getValue());
}
You say your HashMap is not in an Activity, but you won't be able to set the text for a TextView unless you are in an Activity. So let's assume that your HashMap is available from that class. The following should work in your Activity:
TextView testTextView = (TextView) findViewById(R.id.testTextView);
HashMap<String, String> map = otherClassInstance.getMap();
testTextView.setText(map.get("testTextView"));
The fragility here is that if you refactor the ID, you'll also need to remember to change the hard-coded text in your call to setText(). For that reason Gabe's solution is probably better for your case.
Well, the id is converted to an integer and lost to the system at compile time. So that isn't the way to do it. The best idea is to use the tag on each text view: android:tag="a_key". Then when you want to set the text of the view, use getTag to get the tag, and pass it into the hash map to get the associated string.
I know how to assing ID's dynamically by invoking setID(). For the ID's to be unique, I used to utilize ids.xml and pass to setID() ID's from the pre-generated pool of ID's.
Question 1: Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?
I tried to bypass the first issue presented in Question 1 by dynamically assigning each of which an id based on its label's hash (each label is unique), but there is no way to gaurantee that ID's won't be colliding with ID's auto generated in R.java.
Question 1.1: How the ID naming collision can be resolved?
Question 2: Assume I have the ID value of which I assign and generate dynamically. Since the aformentioned ID does not appear in R.id, findViewById() won't be applicable for retrieving the view. Hence, how can the view be retrieved when the ID is known?
Answer 2: You'd be able to retrieve the view by its corresponding ID only after onCreate() has returned control (terminated).
From API level 17 you can get a new id by calling
View.generateViewId()
Details here.
Is there any way to assign ID's without utilizing the ids.xml since I
cannot anticipate how many ID's I will need in runtime?
This guarantees that every view has a unique ID
for(int i =0 ; i < yourIDcount ; i++){
yourView.setId(i);
}
how can the view be retrieved when the ID is known?
View.findViewById(yourView.getId());
can be used to get your view's id, since every view has a unique Id you can get back the view you wanted..
The word dynamic means which is created at runtime, since you assign id in onCreate it is assigned as the views id, since onCreate is called only once an activity is created, you can make sure that the id you assigned stays intact...