android - Passing "this" as root to LayoutInflater.inflate() in a custom component -
i writing code custom component extends linearlayout. include spinner @ top, , number of settings below, depending on spinner set to. i.e., when user selects "apple" on spinner, "color" option appears, , when select "banana" "length" option appears.
since spinner option might have many settings associated it, define each group of settings in layout xml "merge" root tag. call initviews() in each constructor inflate views can add/remove them later.
here code class:
public class schedulepickerview extends linearlayout { protected context context; protected spinner typespinner; protected viewgroup defaultsetters; // viewgroup show when no schedule selected in spinner protected viewgroup simplesetters; // viewgroup show when simpleschedule selected in spinner public schedulepickerview(context context) { super(context); this.context = context; initviews(); } public schedulepickerview(context context, attributeset attr) { super(context, attr); this.context = context; initviews(); } public schedulepickerview(context context, attributeset attr, int defstyle) { super(context, attr, defstyle); this.context = context; initviews(); } private void initviews() { // init typespinner typespinner = (spinner) findviewbyid(r.id.schedulepickerspinner); // init setters (viewgroups show settings various types of schedules layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); // error on line: defaultsetters = inflater.inflate(r.layout.container_schedulesetter_default, this); } }
i error on marked line: "incompatible types: required = viewgroup, found = view". linearlayout extends viewgroup, per this documentation. have tried casting "this" viewgroup, strangely ide greyed-out cast (since, obviously, every linearlayout viewgroup). why there issue?
inflate()
returns view
, you're trying assign more specific viewgroup
variable. it's not this
parent view problematic - need cast on return value:
defaultsetters = (viewgroup)inflater.inflate(...)
Comments
Post a Comment