listview - How can i combine, two OnClickListener in ArrayAdapter (View) on Android -
i have 2 onclicklisteners in arrayadapter under view.
i have onclick row , onclick button, both in same listview. combine them. make 1 onclicklistener both. both go same intent/page when clicked.
not sure how this, without duplicating code.
thanks in advance.
public class listadapter extends arrayadapter { private list<exerciselistmodel> exmodellist; private int resource; private layoutinflater inflater; public listadapter(context context, int resource, list<exerciselistmodel> objects) { super(context, resource, objects); exmodellist = objects; this.resource = resource; inflater = (layoutinflater)getsystemservice(layout_inflater_service); } @override public view getview(final int position, view convertview, viewgroup parent) { viewholder holder = null; if(convertview == null){ holder = new viewholder(); convertview = inflater.inflate(resource, null); holder.extitle = (textview)convertview.findviewbyid(r.id.mytitle); holder.exbody = (textview)convertview.findviewbyid(r.id.mybody); holder.exid = (textview)convertview.findviewbyid(r.id.myid); holder.exdone = (textview)convertview.findviewbyid(r.id.mycount); holder.exicon = (imageview)convertview.findviewbyid(r.id.myicon); holder.btnstart = (button)convertview.findviewbyid(r.id.btnstart); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } //onclick row convertview.setonclicklistener(new view.onclicklistener() { public void onclick(final view v) { string nid = exmodellist.get(position).getnid(); string title = exmodellist.get(position).gettitle(); string body = exmodellist.get(position).getbody(); intent intent = new intent(exerciselistactivity.this, exercisedetailsactivity.class); intent.putextra("nid", nid); intent.putextra("title", title); intent.putextra("body", body); startactivity(intent); } }); //onclick button holder.btnstart.setonclicklistener(new view.onclicklistener() { public void onclick(final view v) { string nid = exmodellist.get(position).getnid(); string title = exmodellist.get(position).gettitle(); string body = exmodellist.get(position).getbody(); intent intent = new intent(exerciselistactivity.this, exercisedetailsactivity.class); intent.putextra("nid", nid); intent.putextra("title", title); intent.putextra("body", body); startactivity(intent); } }); holder.exid.settext("id: " + exmodellist.get(position).getnid()); holder.exbody.settext(exmodellist.get(position).getbody()); holder.extitle.settext(exmodellist.get(position).gettitle()); if(exmodellist.get(position).getexercise_done().equals("[]")){ holder.exdone.settext(exercise_not_done); holder.exicon.setimageresource(r.drawable.exercise_not_started); }else{ string exercise_count = exmodellist.get(position).getexercise_done(); string ex_done = exercise_done + " " + exercise_count + " " + exercise_times_done; holder.exdone.settext(ex_done); holder.exicon.setimageresource(r.drawable.exercise_started); } return convertview; } class viewholder{ private textview extitle; private textview exbody; private textview exid; private imageview exicon; private textview exdone; private button btnstart; } }
inside activity/fragment (where declare listview)
list.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { intent intent = new intent(exerciselistactivity.this, exercisedetailsactivity.class); intent.putextra("nid", nid); intent.putextra("title", title); intent.putextra("body", body); startactivity(intent); }}
Comments
Post a Comment