java - Returning the position in list in Android with OnClickListener on Button -
i have looked on internet , must missing something. task seems simple , have found few other post have yet able mine work. following code (sorry i'm new java , android if have done things not best practice apologize.) anyhow situation,
i have listview populates few text fields database, @ right side of these text boxes have created delete button , record (row) deleted when corresponding delete button pressed. have created onclicklistener button unable obtain position in list view button has been clicked.
--------------------------------------------------- textview textview | delete button --------------------------------------------------- textview textview | delete button ---------------------------------------------------
code:
public class calccursoradapter extends simplecursoradapter implements filterable{ private context mcontext; private listview mlistview; private int mlayout; protected static class viewholder { protected textview text; protected imagebutton button; private int position; } @suppresswarnings("deprecation") public calccursoradapter(context context, int layout, cursor c, string[] from, int[] to) { super(context, layout, c, from, to); this.mcontext = context; this.mlayout = layout; //mlistview = .getlistview(); } @override public void bindview(view view, context context, cursor cursor) { textview summary = (textview)view.findviewbyid(r.id.calctvprice); textview savings = (textview)view.findviewbyid(r.id.calctvsavings); summary.settext(cursor.getstring(cursor.getcolumnindexorthrow("qcfinalprice"))); savings.settext(cursor.getstring(cursor.getcolumnindexorthrow("qcsavings"))); } @override public view newview(context context, cursor cursor, viewgroup parent) { viewholder holder = new viewholder(); layoutinflater inflater = layoutinflater.from(context); view v = inflater.inflate(r.layout.calc_list_item, parent, false); holder.button = (imagebutton) v.findviewbyid(r.id.qcbtndelete); holder.button.setonclicklistener(deletebutton); bindview(v, context, cursor); v.settag(holder); return v; } private onclicklistener deletebutton = new onclicklistener() { public void onclick(view v){ toast.maketext(mcontext.getapplicationcontext(), "test", toast.length_short).show(); } }; public long qcitemid(int position) { return position; } }
i have looked @ many examples , lot of time array adapters, other thing have found people override getview function , under impression perform said task without doing that. also, don't want list item iteslef clickable, button... seems should such simple question have been looking... apologize if covered somewhere , have missed it, have spent last 4 hours searching web trying find fix!
anyhow, appreciated!
thank you.
malachi's link points right direction. there's 1 tiny little detail missing. cursor has method getposition() returns position of cursor in row set. store value in viewholder , you're set:
holder.position = c.getposition();
in onclicklistener retrieve viewholder , position contains:
viewholder holder = (viewholder) v.gettag(); int position = holder.position;
the position can used retrieve underlying data:
object mydata = getitem(position);
Comments
Post a Comment