android - Dynamic layout: how to get specific view -
i have activity in need add linearlayout dynamically, code (because depends on user input). so, here have done:
layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); linearlayout lyt = (linearlayout) inflater.inflate(r.layout.row_edit, mlytrows); textview tvname = (textview) lyt.findviewbyid(r.id.name_textview); tvname.settext(user.getname());
where r.layout.row_edit
linearlayout in there views, including textview, whereas mlytrows
referenced linearlayout (defined in xml of ui), in want add row_edit
layout.
based on user input, repeat code several times , here problem: when try reference textview, aways textview of first linearlayout have added.
why? how can solve it, please?
take closer @ the documentation.
if specify second argument in inflate()
, method returns parent, not inflated view.
hence, lyt
parent , findviewbyid
returns first element finds r.id.name_textview
id.
you might want do
layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); linearlayout lyt = (linearlayout) inflater.inflate(r.layout.row_edit, null); textview tvname = (textview) lyt.findviewbyid(r.id.name_textview); tvname.settext(user.getname()); mlytrows.addview(lyt);
and in viewgroup can see there several addview
methods place view want.
Comments
Post a Comment