java - How to access data in main Activity from a button pressed in listView? -
so have listview custom adapter , want whenever press button listen onitemclicklistener mainactivity.
so question pass while calling onclick on button present in listview can read main activity?
@override public view getview(final int pos, view convertview, viewgroup parent) { holder holder = new holder(); final customproductadapter p=this; final view rowview; rowview = inflater.inflate(r.layout.product_list_test, null); holder.productview=(textview) rowview.findviewbyid(r.id.productview); holder.descview=(textview) rowview.findviewbyid(r.id.descview); holder.imageview=(imageview) rowview.findviewbyid(r.id.imageview); holder.productcost=(textview) rowview.findviewbyid(r.id.productcost); holder.ibutton=(imagebutton) rowview.findviewbyid(r.id.imagebutton); holder.ibutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //what put here? } }); holder.productview.settext(pronames[pos]); holder.descview.settext(r.string.description_product); holder.productcost.settext("0.00"); imageview image =holder.imageview; imageloader.displayimage(images[pos],image); rowview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(context, "you clicked "+pronames[pos], toast.length_short).show(); } }); return rowview; }
so can read here this(i dont know if corect way)
listview = (listview)findviewbyid(r.id.listview); listview.setadapter(new customproductadapter(this, images, pronames1, desc)); adapterview.onitemclicklistener mmessageclickedhandler = new adapterview.onitemclicklistener() { public void onitemclick(adapterview parent, view v, int position, long id) { // in response click long ids=v.getid(); if(ids==r.id.imagebutton){ toast.maketext(getbasecontext(),"button clicked @ "+position,toast.length_short).show(); } } }; listview.setonitemclicklistener(mmessageclickedhandler);
in mainactivity.java. beginner please me out if not doing correct.
using interface communicate 1 of ways achieve this.
//create interface interface buttonclicknotify{ void onbuttonclick(int position); }
then implement in main activity
public calss mainactivity extends appcompatactivity implements buttonclicknotify{ //override interface method @override public void onbuttonclick(int position){ //do } }
then call interface adapter.
//declare instance variable buttonclicknotify buttonnotify; //initiate iterface in constructor public mylistadapter(context context /*and other parameters*/){ //after other constructor methods including calling super try{ buttonnotify=(mainactivity)context; }catch(throwable e){ //interface not implemented } } //at button onclick holder.ibutton.setonclicklistener(new view.onclicklistener{ @overrride public void onclick(view v){ try{ buttonnotify.onbuttonclick(position); }catch (throwable e){ //interface can null } } });
and don't need have onclick
on rowview
listview.setonitemclicklistener
same.
Comments
Post a Comment