java - Add dynamic data to onClickListener of TextViews in Table Layout -
i have made dynamic table rows 2 textviews in each row different id (local variable in java file - see code). want open activity image name using same id of textview.(which can send using intent)
but first, tried print toast same id assigned textview.
// java code int id = 3; (int = 1; <= 3; i++) { tablerow row = new tablerow(tableactivity.this); // add layouts new row textview txt1 = new textview(tableactivity.this); txt1.settext("" + id); txt1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext(getapplicationcontext(), "" + id, toast.length_short).show(); } }); id++; textview txt2 = new textview(tableactivity.this); txt2.settext("" + id); id++; row.addview(txt1); row.addview(txt2); // add new row tablelayout: tablelayout table = (tablelayout) findviewbyid(r.id.tablelayout1); table.addview(row); }
with xml code,
<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tablelayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:stretchcolumns="0,1,2,3" tools:context="com.himanshu.fileimage.tableactivity" > <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="column1" android:textcolor="#000000" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="column2" android:textcolor="#000000" /> </tablerow>
the error comes toast id should final variable inside inner class.
so can possible solution problem?
that because trying access variable local method of outer class inside of method belonging class (view.onclicklistener) have defined inline. part can solved declaring id variable outside of method member class of above code.
however, still not solve problem trying print value of id "when" textview clicked value of id has changed (by incrementing it) right after setting onclicklistener @ time of rendering different views. toast incorrect id value.
to correct 1 want textview being touched know id , want set id tag textview - txt1.settag(id); right before change/increment id's value set next textview.
now in onclick view.onclicklistener in maketoast method pass v.gettag() 2nd parameter in place of id , desired output.
edit: btw, setting id tag on views doesn't have class variable, can local method have in above code. first paragraph explain why couldn't access inside onclick
Comments
Post a Comment