android - how to create multiselection in listview? -
i using listview in android app. use checkbox in listview , want know: how can create multiselection , how can show selected items in activity button click event. following code snippet:
public class home extends listactivity { private progressdialog pdialog; // url contacts json private static string url = "http://api-11hr.anovatesoft.com/v1/list"; // json node names private static final string tag_contacts = "contacts"; private static final string tag_username = "username"; 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_contact_number = "contactnumber"; private static final string tag_postal_code = "postalcode"; private static final string tag_image = "image"; sessionmanager session; private double latitude; private double longitude; private string apikey; private string status; private imageview back; private checkbox check; gpstracker gps; listview listview; private toolbar toolbar; // declaring toolbar object // contacts jsonarray jsonarray contacts = null; // hashmap listview arraylist<hashmap<string, string>> contactlist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.home); findviewsbyid(); listview.setchoicemode(listview.choice_mode_multiple); check=(checkbox)findviewbyid(r.id.ck); alertdialog.builder alertdialog2 = new alertdialog.builder( home.this); // setting dialog title alertdialog2.settitle("too quiet?"); // setting dialog message alertdialog2.setmessage("follow favourite merchants , see deals they're launching @ 11th hour!"); // setting positive "yes" btn alertdialog2.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // write code here execute after dialog dialog.cancel(); } }); alertdialog2 .show(); = (imageview) findviewbyid(r.id.back); back.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent intent = new intent(home.this, resultactivity.class); startactivity(intent); } }); gps_func(); // session class instance session = new sessionmanager(getapplicationcontext()); // user data session hashmap<string, string> user = session.getuserdetails(); // apikey // calling application class (see application tag in androidmanifest.xml) final globalclass globalvariable = (globalclass) getapplicationcontext(); //set name , email in global/application context apikey = globalvariable.getapikey(); latitude = globalvariable.getlatitude(); longitude = globalvariable.getlongitude(); status = globalvariable.getstatus(); 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 string cost = ((textview) view.findviewbyid(r.id.email)) .gettext().tostring(); // starting single contact activity intent in = new intent(getapplicationcontext(), adds.class); in.putextra(tag_email, cost); startactivity(in); } }); // calling async task json new getcontacts().execute(); } private void gps_func() { gps = new gpstracker(home.this); // check if gps enabled if (gps.cangetlocation()) { double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); // \n new line // toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show(); log.d("tag", "longitude:\n" + longitude + "\n latitude: \n" + latitude); } else { // can't location // gps or network not enabled // ask user enable gps/network in settings gps.showsettingsalert(); if (gps.cangetlocation()) { double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); // \n new line // toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show(); log.d("tag", "longitude:\n" + longitude + "\n latitude: \n" + latitude); } } } @override protected void onrestart() { super.onrestart(); gps_func(); } /** * 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(home.this); pdialog.setmessage("please wait...."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler1 sh = new servicehandler1(apikey, latitude, longitude); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler1.post); 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 username = c.getstring(tag_username); string name = c.getstring(tag_name); string email = c.getstring(tag_email); string address = c.getstring(tag_address); string gender = c.getstring(tag_contact_number); string postalcode = c.getstring(tag_postal_code); // tmp hashmap single contact hashmap<string, string> contact = new hashmap<string, string>(); // adding each child node hashmap key => value contact.put(tag_email, email); // 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( home.this, contactlist, r.layout.list_item1, new string[]{tag_email, }, new int[]{ r.id.email,}); setlistadapter(adapter); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } private void findviewsbyid() { check = (checkbox) findviewbyid(r.id.ck); = (imageview) findviewbyid(r.id.back); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
here give simple project can used multiple selection in listview.
public class mainactivity extends activity { private listview llchb; private string[] data = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; private arraylist<string> arrdata=null; private arraylist<inforowdata> infodata; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); arrdata=new arraylist<string>(); arrdata.add("a"); arrdata.add("b"); arrdata.add("c"); arrdata.add("d"); arrdata.add("e"); arrdata.add("f"); arrdata.add("g"); arrdata.add("h"); arrdata.add("i"); arrdata.add("j"); arrdata.add("k"); arrdata.add("l"); arrdata.add("m"); arrdata.add("n"); arrdata.add("o"); arrdata.add("p"); llchb = (listview) findviewbyid(r.id.llchb); infodata = new arraylist<inforowdata>(); (int = 0; < data.length; i++) { infodata.add(new inforowdata(false, i)); // system.out.println(i); //system.out.println("data == "+data[i]); } llchb.invalidate(); llchb.setadapter(new myadapter()); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } public class myadapter extends baseadapter { @override public int getcount() { // todo auto-generated method stub return data.length; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub view row = null; row = view.inflate(getapplicationcontext(), r.layout.row, null); textview tvcontent=(textview) row.findviewbyid(r.id.tvcontent); //tvcontent.settext(data[position]); tvcontent.settext(data[position]); //system.out.println("the text here like.. == "+tvcontent.gettext().tostring()); final checkbox cb = (checkbox) row .findviewbyid(r.id.chbcontent); cb.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (infodata.get(position).isclicked) { infodata.get(position).isclicked = false; } else { infodata.get(position).isclicked = true; } for(int i=0;i<infodata.size();i++) { if (infodata.get(i).isclicked) { system.out.println("selectes == "+ data[i]); } } } }); if (infodata.get(position).isclicked) { cb.setchecked(true); } else { cb.setchecked(false); } return row; } } }
inforowdata class here:
public class inforowdata { public boolean isclicked=false; public int index; /*public string fanid; public string stramount;*/ public inforowdata(boolean isclicked,int index/*,string fanid,string stramount*/) { this.index=index; this.isclicked=isclicked; /*this.fanid=fanid; this.stramount=stramount;*/ } }
activity_main.xml here:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <listview android:id="@+id/llchb" android:layout_width="match_parent" android:layout_height="wrap_content" > </listview>
row.xml here:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlmain" android:layout_width="fill_parent" android:layout_height="fill_parent" > <relativelayout android:id="@+id/rlinner" android:layout_width="fill_parent" android:layout_height="50dp" > <textview android:id="@+id/tvcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="20dp" android:text="medium text" android:textcolor="#000000" /> <checkbox android:id="@+id/chbcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="10dp" /> </relativelayout> </relativelayout>
hope work :)
Comments
Post a Comment