简体   繁体   中英

How to use an id that was set programmatically

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 ie 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM