android - How do I get the position of a togglebutton that is clicked in a gridview? -
how position of android togglebutton clicked in gridview?
if use following example code dont toast message of position.
public class hellogridview extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(new togglebuttonadapter(this)); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { toast.maketext(hellogridview.this, "" + position, toast.length_short).show(); } }); }
}
and toggle adapter:
public class togglebuttonadapter extends baseadapter { private context mcontext; public togglebuttonadapter(context c) { mcontext = c; } @override public int getcount() { return 5; } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view view; layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); if (convertview == null) { // reuse if exists view = (view) inflater.inflate(r.layout.items, null); } else { view = (view) convertview; } togglebutton togglebutton = (togglebutton) view.findviewbyid(r.id.togglebutton1); textview textview = (textview) view.findviewbyid(r.id.textview1); textview.settext("toggle button " + position); /*togglebutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { togglebutton t = (togglebutton) v.findviewbyid(r.id.togglebutton1); toast.maketext(mcontext, boolean.tostring(t.ischecked()), toast.length_short).show(); } });*/ return view; }
}
uncomment toggle listener play part , show me how position. there way inject data toggle button grid view being constructed later reference? couldn't find way reading togglebutton javadoc.
thanks much!
edit:
oops! here layouts:
main.xml
<gridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnwidth="90dp" android:numcolumns="auto_fit" android:verticalspacing="10dp" android:horizontalspacing="10dp" android:stretchmode="columnwidth" android:gravity="center" > </gridview>
items.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <textview android:text="some label" android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp"/> <togglebutton android:id="@+id/togglebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp"/> </linearlayout> </linearlayout>
in getview method, define final int pos = position;
and in onclicklistener,
togglebutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { togglebutton t = (togglebutton) v.findviewbyid(r.id.togglebutton1); toast.maketext(mcontext, boolean.tostring(t.ischecked()) + " : " + string.valueof(pos), toast.length_short).show(); } });
works fine, checked :)
this works because, onclicklistener create in getview method local class, retains local copy of variables accesses of outer class (this reason need declare pos final). instance of class attached toggle button returned method, , lifetime attached lifetime of toggle button.
more local classes here
Comments
Post a Comment