android - Display ListView of selected data to next Activity in textView -
in listview
here have contacts check box. when select 2 contacts list , hit button selected list's value should display in next activity. how can this?
its activity class :
public class contactlistactivity extends activity implements onitemclicklistener { private listview listview; private list<contactbean> list = new arraylist<contactbean>(); @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview = (listview) findviewbyid(r.id.list); listview.setonitemclicklistener(this); cursor phones = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null, null, null, null); while (phones.movetonext()) { string name = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); string phonenumber = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.number)); contactbean objcontact = new contactbean(); objcontact.setname(name); objcontact.setphoneno(phonenumber); list.add(objcontact); } phones.close(); contanctadapter objadapter = new contanctadapter(contactlistactivity.this, r.layout.alluser_row, list); listview.setadapter(objadapter); if (null != list && list.size() != 0) { collections.sort(list, new comparator<contactbean>() { @override public int compare(contactbean lhs, contactbean rhs) { return lhs.getname().compareto(rhs.getname()); } }); alertdialog alert = new alertdialog.builder(contactlistactivity.this).create(); alert.settitle(""); alert.setmessage(list.size() + " contact found!!!"); alert.setbutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); alert.show(); } else { showtoast("no contact found!!!"); } } private void showtoast(string msg) { toast.maketext(this, msg, toast.length_short).show(); } @override public void onitemclick(adapterview<?> listview, view v, int position, long id) { contactbean bean = (contactbean) listview.getitematposition(position); showcalldialog(bean.getname(), bean.getphoneno()); } private void showcalldialog(string name, final string phoneno) { alertdialog alert = new alertdialog.builder(contactlistactivity.this).create(); alert.settitle("call?"); alert.setmessage("are sure want call " + name + " ?"); alert.setbutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); alert.setbutton2("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { string phonenumber = "tel:" + phoneno; intent intent = new intent(intent.action_call, uri.parse(phonenumber)); startactivity(intent); } }); alert.show(); }
and adapter class hold data is
public class contanctadapter extends arrayadapter<contactbean> { private activity activity; private list<contactbean> items; private int row; private layoutinflater inflater = null; public contanctadapter(activity act, int row, list<contactbean> items) { super(act, row, items); this.activity = act; this.row = row; this.items = items; this.inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service); } @override public view getview(final int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(row, null); holder.tvname = (textview) convertview.findviewbyid(r.id.tvname); holder.tvphoneno = (textview) convertview.findviewbyid(r.id.tvphone); holder.checkbox = (imageview) convertview.findviewbyid(r.id.img_checkbox); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } if ((items == null) || ((position + 1) > items.size())) return convertview; contactbean objbean = items.get(position); holder.checkbox.setselected((objbean.getisselected() == 1) ? true : false); if (holder.tvname != null && null != objbean.getname() && objbean.getname().trim().length() > 0) { holder.tvname.settext(html.fromhtml(objbean.getname())); } if (holder.tvphoneno != null && null != objbean.getphoneno() && objbean.getphoneno().trim().length() > 0) { holder.tvphoneno.settext(html.fromhtml(objbean.getphoneno())); } holder.checkbox.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { items.get(position).isselected = (v.isselected()) ? 0 : 1; notifydatasetchanged(); } }); return convertview; } public class viewholder { public textview tvname, tvphoneno; private imageview checkbox; } }
there multiple ways achieve :
method 1:
use static class setter , getter method:
create static class , set values first activity , value second activity
method 2:
post values through intent
method 3:
use database store data 1 activity , data other activity
method 4:
use shared preference
example:
post values using intent like this
post values in shared preference
another tutorial shared preference
Comments
Post a Comment