android - Why root View return from inflate() does not match with View returned from findViewById()? -
as new-bee again in trouble understanding basics of inflate().
here xml file -
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" android:id="@+id/linearlayout" android:orientation="vertical"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/textview" /> </linearlayout>
below little basic code -
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); linearlayout ly = (linearlayout)findviewbyid(r.id.linearlayout); log.i("system.out ","linear layout = " + ly); view view=getlayoutinflater().inflate(r.layout.activity_main,null); linearlayout ly1 = (linearlayout)findviewbyid(r.id.linearlayout); log.i("system.out ","linear layout = " + view); log.i("system.out ","linear layout = " + ly1); }
and output:
05-10 14:30:33.446: i/system.out(26806): linear layout = android.widget.linearlayout@41e28848 05-10 14:30:33.446: i/system.out(26806): linear layout = android.widget.linearlayout@41e29740 05-10 14:30:33.446: i/system.out(26806): linear layout = android.widget.linearlayout@41e28848
what understand 1st , 3rd line of output once call setcontentview()
inflating , hence view object in memory after call method. therefore on calling findviewbyid()
, return same object of linearlayout view both time in code block. (ly isequalto ly1)
but, why address of linearlayout object in 2nd line of output different, linear layout = android.widget.linearlayout@41e29740
?
code responsible -
view view=getlayoutinflater().inflate(r.layout.activity_main,null);
i thought return root view, in case linearlayout.
if r.layout.activity_main
inflated , there no change in layout(neither addition or removal of view/viewgroup), why address of object(view & ly1) not match?
i tried -
view view=getlayoutinflater().inflate(r.layout.activity_main,null); setcontentview(view); linearlayout ly1 = (linearlayout)findviewbyid(r.id.linearlayout); log.i("system.out ","linear layout = " + view); log.i("system.out ","linear layout = " + ly1);
and got -
i/system.out(2603): linear layout = android.widget.linearlayout@41e09e10 i/system.out(2603): linear layout = android.widget.linearlayout@41e09e10
why ly1 , view object represent same address in case ?
inflate()
return new view
object. ly
, ly1
same object . why expecting differently?
from inflate doc
inflate new view hierarchy specified xml resource.
Comments
Post a Comment