java - How to make .setText() of a GridView item (textview) onItemSelected? -
java - How to make .setText() of a GridView item (textview) onItemSelected? -
i'm beginner in android. here want alter text of item in gridview when clicked, want know how can done....
here code:
public class mainactivity extends activity implements onitemclicklistener { textview tv; button b; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gv = (gridview) findviewbyid(r.id.gridview1); b = (button) findviewbyid(r.id.button1); gv.setadapter(new custom(this)); gv.setonitemclicklistener(this); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub } }
custom class:
public class custom extends baseadapter { private layoutinflater li; context ctx; button b2; int v = 0; string items = "krishna"; public custom(context c) { ctx = c; li=(layoutinflater)c.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub homecoming items.length(); } @override public object getitem(int position) { // todo auto-generated method stub homecoming items.charat(position); } @override public long getitemid(int position) { // todo auto-generated method stub homecoming position; } @override public view getview(int position, view convertview, viewgroup parent) { view b1=convertview; if(convertview==null){ b1=li.inflate(r.layout.c_layout, null); } b2 =(button) b1.findviewbyid(r.id.button2); b2.settext(string.valueof(items.charat(position))); b2.setheight((int) 60); b2.setgravity(gravity.center); b2.setclickable(true); if ((position % 2) == 0) { katre(); } else { katre2(); } homecoming b1; } public void katre() { b2.setbackgroundresource(extracted()); } private int extracted() { homecoming r.color.button_material_dark; } public void katre2() { b2.setbackgroundresource(r.color.material_blue_grey_900); } }
and activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" android:layout_gravity="center_horizontal"/> <gridview android:id="@+id/gridview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numcolumns="7" android:stretchmode="columnwidth" > </gridview> </linearlayout>
and @ lastly c_layout.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="button" /> </relativelayout>
please help me...
want alter text of item in gridview when clicked
use view
parameter of onitemclick
access button clicked item:
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { button clickeditem =(button) view.findviewbyid(r.id.button2); clickeditem.settext("clicked"); }
edit:
or can adding onclicklistener
button in getview
method:
b2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ((button)v).settext("clicked"); } });
java android onclicklistener android-gridview settext
Comments
Post a Comment