list view with single contact activity in android -
// json node names private static final string tag_contacts = "contacts"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_email = "email"; private static final string tag_address = "address"; private static final string tag_gender = "gender"; private static final string tag_phone = "phone"; private static final string tag_phone_mobile = "mobile"; private static final string tag_phone_home = "home"; private static final string tag_phone_office = "office"; // contacts jsonarray jsonarray contacts = null; // hashmap listview arraylist<hashmap<string, string>> contactlist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.search_list); contactlist = new arraylist<hashmap<string, string>>(); listview lv = getlistview(); // listview on item click listener lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem int itemposition = position; string name = ((textview) view.findviewbyid(r.id.name)) .gettext().tostring(); string cost = ((textview) view.findviewbyid(r.id.email)) .gettext().tostring(); // string add = ((textview) view.findviewbyid(r.id.address)) // .gettext().tostring(); string description = ((textview) view.findviewbyid(r.id.mobile)) .gettext().tostring(); string price =((textview) view.findviewbyid(r.id.price)).gettext().tostring(); string amount=((textview) view.findviewbyid(r.id.mobile_labels)).gettext().tostring(); //string sweet = ((textview) view.findviewbyid(r.id.home)) // .gettext().tostring(); // starting single contact activity intent in = new intent(searchactivity.this, singlecontactactivity.class); in.putextra(tag_name, name); in.putextra(tag_email, cost); //in.putextra(tag_phone_mobile,description); //in.putextra(tag_phone_home,price); // in.putextra(tag_phone_office,amount); //in.putextra(tag_address,add); //in.putextra(tag_phone_mobile, description); // in.putextra(tag_phone_home,sweet); startactivity(in); } }); // calling async task json new getcontacts().execute(); } /** * async task class json making http call * */ private class getcontacts extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(searchactivity.this); pdialog.setmessage("please wait..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node contacts = jsonobj.getjsonarray(tag_contacts); // looping through contacts (int = 0; < contacts.length(); i++) { jsonobject c = contacts.getjsonobject(i); string id = c.getstring(tag_id); string name = c.getstring(tag_name); string email = c.getstring(tag_email); // string add = c.getstring(tag_address); string gender = c.getstring(tag_gender); // phone node json object jsonobject phone = c.getjsonobject(tag_phone); string mobile = phone.getstring(tag_phone_mobile); string home = phone.getstring(tag_phone_home); string office = phone.getstring(tag_phone_office); // tmp hashmap single contact hashmap<string, string> contact = new hashmap<string, string>(); // adding each child node hashmap key => value contact.put(tag_id, id); contact.put(tag_name, name); contact.put(tag_email, email); // contact.put(tag_address,add); contact.put(tag_phone_mobile, mobile); contact.put(tag_phone_home,home); contact.put(tag_phone_office,office); // adding contact contact list contactlist.add(contact); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( searchactivity.this, contactlist, r.layout.list_item, new string[] { tag_name, tag_email, tag_phone_mobile ,tag_phone_home,tag_phone_office }, new int[] { r.id.name, r.id.email, r.id.mobile,r.id.price ,r.id.mobile_labels}); setlistadapter(adapter); } } }
here list. in getting name,email , mobile number. in list when select name in list open new activity in activity need name, email, phone, address, gender, office. how should pass in next activity.
here next activity code.
private static final string tag_name = "name"; private static final string tag_email = "email"; private static final string tag_phone_mobile = "mobile"; private static final string tag_phone_home = "home"; private static final string tag_phone_office = "office"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_single_contact); // getting intent data intent in = getintent(); // json values previous intent string name = in.getstringextra(tag_name); string email = in.getstringextra(tag_email); string mobile = in.getstringextra(tag_phone_mobile); string home = in.getstringextra(tag_phone_home); string office= in.getstringextra(tag_phone_office); // displaying values on screen textview lblname = (textview) findviewbyid(r.id.name_label); textview lblemail = (textview) findviewbyid(r.id.email_label); textview lblmobile = (textview) findviewbyid(r.id.mobile_label); textview lblmobiles = (textview) findviewbyid(r.id.mobile_labels); textview lbloffice = (textview) findviewbyid(r.id.mobile_office); lblname.settext(name); lblemail.settext(email); lblmobile.settext(mobile); lblmobiles.settext(home); lbloffice.settext(office); } }
it's better if have name in listview. on selecting name in list further details should displayed, instance: name, phone, email, address, gender.
pass them through intent used start new activity. when generate intent can bundle information , pass extra. information should in bundle when new activity starts. that's easiest way.
there's lots of information in links, , google has tutorials on how use intents well.
hope helps!
Comments
Post a Comment