java - how to add different activity for each row in listview in android -
i know how add activity listview using intent , can see in below code added back.class mainactivity.java customadapter.java file. when click on each row of listview everytime open 1 activity(back.class). want add different activity (leg.class, abs.class, chest.class etc) mainactivity.java using intent when click on each row of listview open different activity. don't know how it?
this mainactitvity.java file
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); string[] excercise = {"back day", "legs day", "abs day", "chest day", "shoulder day", "arms day"}; final int[] imgs = {r.drawable.back, r.drawable.leg, r.drawable.abs, r.drawable.chest, r.drawable.shoulder, r.drawable.arms}; listadapter saruadapter = new customadapter(this, excercise, imgs); listview sarulistview = (listview) findviewbyid(r.id.sarelistview); sarulistview.setadapter(saruadapter); // intent intent = new intent(); // startservice(intent); sarulistview.setonitemclicklistener( new android.widget.adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // string excercise = string.valueof(parent.getitematposition(position)); // toast.maketext(mainactivity.this, excercise, toast.length_long).show(); intent = new intent(mainactivity.this, back.class); startactivity(i); } } ); } }
this customadapter.java file
public class customadapter extends arrayadapter<string>{ private int[] imgs; public customadapter(context context, string[] excercise, int[] imgs) { super(context, r.layout.custom_row, excercise); this.imgs = imgs; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater saruinflator = layoutinflater.from(getcontext()); view coustomview = saruinflator.inflate(r.layout.custom_row, parent, false); string excerciseitem = getitem(position); textview saruview = (textview) coustomview.findviewbyid(r.id.sareview); imageview saruimg = (imageview) coustomview.findviewbyid(r.id.sareimage); saruview.settext(excerciseitem); saruimg.setimageresource(imgs[position]); return coustomview; } }
this back.java file
public class extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_bacon2); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); bundle = getintent().getextras(); if(back == null) { return; } } }
use switch condition , use position open new activity.
sarulistview.setonitemclicklistener( new android.widget.adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // string excercise = string.valueof(parent.getitematposition(position)); // toast.maketext(mainactivity.this, excercise, toast.length_long).show(); switch (position) { case 0: intent i0 = new intent(mainactivity.this, back.class); startactivity(i0); break; case 1: intent i1 = new intent(mainactivity.this, leg.class); startactivity(i1); break; case 2: intent i2 = new intent(mainactivity.this, abs.class); startactivity(i2); break; case 3: intent i3 = new intent(mainactivity.this, chest.class); startactivity(i3); break; } } } );
Comments
Post a Comment