Android Gridview Selection not working -
when clicking icon in gridview, showing " clicked this". instead need go new page corresponding icon.
here code
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gv=(gridview) findviewbyid(r.id.gridview1); gv.setadapter(new customadapter(this, prgmnamelist,prgmimages)); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
}
and customadapter.java
public customadapter(mainactivity mainactivity, string[] prgmnamelist, int[] prgmimages) { result=prgmnamelist; context=mainactivity; imageid=prgmimages; inflater = ( layoutinflater )context. getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return result.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } public class holder { textview tv; imageview img; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub holder holder=new holder(); view rowview; rowview = inflater.inflate(r.layout.programlist, null); holder.tv=(textview) rowview.findviewbyid(r.id.textview1); holder.img=(imageview) rowview.findviewbyid(r.id.imageview1); holder.tv.settext(result[position]); holder.img.setimageresource(imageid[position]); rowview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext(context, "you clicked "+result[position], toast.length_long).show(); } }); return rowview; }
}
what changes should make ?
implement adapterview.onitemclicklistener
in main activity , set listener gridview gridview.setonitemclicklistener(this);
. better handle click events on each item.
also refer link
Comments
Post a Comment