I have created a table row and set its ID. Now i want to find the row so i can put a fragment inside of it. So how can i find this row? Usually when i set the Id in xml i can easily find the row by R.id.tableRow1 . But since i set it programmatically this doesnt work.
This is my code:
TableRow tr1 = new TableRow(myView.getContext());
tr1.setId(1);
ChildFragmentIntro cfi = new ChildFragmentIntro();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(tr1.getId(), cfi); //This is where it goes wrong, tr1.getId()
transaction.commit();
Im i setting the id in a wrong way or is tr1.getId() the wrong method to retrieve it? If i do it in XML i.e. create a table row in the layout, set its id with the usual #+id/xxx, and use transaction.add(R.id.xxx, cfi); than it works fine. But now i want to do it programmatically.
You can use View.setId() to set an ID to your newly generated View. On API 17+ you can also generate a unique ID using View.generateViewId();
Also see this: Android: View.setID(int id) programmatically - how to avoid ID conflicts?
Related
Please don't be so strict, I never did this before.
I have database with some hobbies and their id.
What I need is dynamically create some quantity of TextView for every hobby. With this no problem :)
But then i need to set onClickListener on each of this textView, and if one of them was clicked - get id for this hobby from database.
Any ideas please!
maybe setTag(id) with id of hobby and inside OnClickListener obtain this tag by (int) v.getTag()
I have the code below in my adapter that creates dynamically EditTexts and set IDs using the method "et_settingValue.setId(setting.getId());".
An Editbox is created in every instace of the class Setting and it also contains a variable to store its id.
This part is already working properly, but now I need to access all those created EditTexts by ID and get its data. If possible, I would like to avoid creating another array to store the EditTexts because I already have their IDs.
Is there any way to do it using the dynamic IDs I already have?
EditText et_settingValue = (EditText) view.findViewById(R.id.et_settingValue);
et_settingValue.setText(setting.getValue().trim());
et_settingValue.setId(setting.getId());
Update 1
In my activity, I am trying to do this:
EditText et = new EditText(listView.getContext());
//loop to get each object child
settingConfig.getConfigName(); //ok
settingConfig.getConfigValue(); //ok
settingConfig.getConfigId(); //ok
et = (EditText) listView.findViewById(settingConfig.getConfigId()); //not working
et.getText(); // off course it will not working
Many thanks
ListView use a RecycleBin to reuse the view created from Adapter, so ListView will only contain a few child view, and you cannot find all EditText in the ListView.
To solve your problem, you should use a Map to record the value of all EditText. Add TextWatcher to each of them, and refresh the value in the Map on text changed.
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...
I ask this because I have exceptions from reports (from users from the market), mentioning that I have duplicated views with id 0x2 (or 0x3).
Since all my generated ids are really big, I think that the views with duplicated ids are views with no specifically defined ids.
My question is what are the ids of the views, that the developer hasn't explicitly assigned ids to them.
Thanks in advance,
Danail
The AAPT constantly updates your R file to generate unique hexadecimal values for each of your own IDs. In terms of IDs YOU create, they only need to be unique within the parent viewgroup. As always please post your stacktrace.
According to the source code, a View for which you haven't set an ID, has an ID of -1.
public static final int NO_ID = -1;
I would say no id is created if you do not specify an id to a view. Try creating a very simple application and create components with no id's , you'll notice that no id's are created in the R.java file.
I have a for loop that each time prints out a row with a TextView within. I want this TextView to be clickable, and when clicked it is supposed to start another activity and send an ID with the Intent. But it's this that I can't manage to work. All links shows up nicely in a table and the textviews are clickable, but all links sends me to the next activity with the same ID, the ID of the last link.
Here is a simplified version of the content of the for loop:
TextView title = new TextView(this);
title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
title.setText(titleLine);
title.setClickable(true);
title.setId(rows);
rows++;
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(a.this, b.class);
myIntent.putExtra("id", idLine);
a.this.startActivity(myIntent);
}
});
contRight.addView(title);
I suppose the problem is that all textViews looks the same, so the last setOnClickListener works on all links. However, I have added an ID to each title, with this code: title.setId(rows);, so they are all supposed to be unique.
Anybody who can give some help? :) Thanks!
Shouldn't idLine be v.getId()? It's good for this value to be as "fresh" as possible so that you aren't reading an old value: in this case, the value of the last row which had probably been lurking in idLine. Hence, getting the value from the view directly is the safest method.
Also, the "tag" might be a better place to store information than the id. You can access the tag, and indeed store any number of arguments as tags on any view, without changing the id:
title.setTag(rows);
and then later:
intent.putExtra((Integer) v.getTag());
To store multiple objects (with keys) you can use:
title.setTag("rowid", rows);
and then retrieve it using:
intent.putExtra((Integer) v.getTag("rowid"));
and then you could add additional keys if you requirements changed.
In any case, the intention of the tag is to store information for later retrieval, whereas the id is meant to serve as an identifier for finding views in a hierarchy.