java - How to know in which item a button was clicked -
so far have list view custom adapter, , each item in list has button. im confused; im trying following:
when user clicks on button(a delete button) in item in list, want know in item button clicked can know item delete-how implement this? ive seen setting tags, im still lost.
i have tried reach button list layout main activity, , cannot reference it.
please can give me detailed description on how want thanks.
added adapter code:
public class locationadapter extends baseadapter{ string [] n; context context; string[] a; private static layoutinflater inflater=null; public locationadapter(mainactivity mainactivity, string[] names, string[] addresses) { // todo auto-generated constructor stub n=names; context=mainactivity; a=addresses; inflater = ( layoutinflater )context. getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return n.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 name; textview address; imagebutton ib; } @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.rowlayout2, null); holder.name =(textview) rowview.findviewbyid(r.id.edittextname); holder.address =(textview) rowview.findviewbyid(r.id.edittextaddress); holder.ib = (imagebutton) rowview.findviewbyid(r.id.delete); holder.name.settext(n[position]); holder.address.settext(a[position]); holder.ib.settag(convertview); rowview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext(context, "you clicked "+n[position], toast.length_long).show(); } }); return rowview; }
}
implement onclicklistener
delete
button.
when delete
button clicked, remove row in data source @ position
, , call notifydatasetchanged()
@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.rowlayout2, null); holder.name =(textview) rowview.findviewbyid(r.id.edittextname); holder.address =(textview) rowview.findviewbyid(r.id.edittextaddress); holder.ib = (imagebutton) rowview.findviewbyid(r.id.delete); holder.name.settext(n[position]); holder.address.settext(a[position]); holder.ib.settag(convertview); //add on-click of delete button holder.ib.setonclicklistener(new onclicklistener(){ //delete row in data source specified @ position }); rowview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext(context, "you clicked "+n[position], toast.length_long).show(); } }); return rowview; }