0

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.

user3711421
  • 1,658
  • 3
  • 20
  • 37

1 Answers1

1

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?

Community
  • 1
  • 1
VM4
  • 6,321
  • 5
  • 37
  • 51
  • Im having no problems to set the id, i just cannot retrieve it. It seems like i have to add a resource file named ids.xml and put the id there. But i was hoping that there were some other way. – user3711421 Aug 09 '14 at 15:26
  • 1
    Sorry about that, didn't read your question fully. R.id.someid is only an int. You can store your generated integer in wherever (preferences, database, Application class) and just do someLayout.findViewById(mySavedInteger); – VM4 Aug 09 '14 at 15:28
  • No, im sorry ;):P. I didnt read the Logcat closely enough. There were no problem with the above code. The error were in another fragment. Time for a break :/. – user3711421 Aug 09 '14 at 15:45