Get value from Android listview and put it in extra -


in code below created method is: put variable in putextra can in new activity. problem in new activity gave me first value(numtelephone) first element in list. know because of getitem(0), how can value( numtelephone ) every element list?

public class listclient extends activity {     string myjson,test;    textview numt;     private static final string tag_results="result";     private static final string tag_nomclient = "nomclient";     private static string tag_numtelephone ="numtelephone";     private static final string tag_depart ="depart";     private static final string tag_destination ="destination";     private static final string tag_nbrpersonne ="nbrpersonne";       jsonarray peoples = null;     arraylist<hashmap<string, string>> personlist;  listview list; @suppresslint("newapi")     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_list_client);         list = (listview) findviewbyid(r.id.list);         personlist = new arraylist<hashmap<string,string>>();         getdata();     }      protected void showlist(){             //json parsing etc...              listadapter adapter = new simpleadapter(                     listclient.this, personlist, r.layout.list_item,                     new string[]{tag_nomclient,tag_numtelephone,tag_depart,tag_depart,tag_nbrpersonne},                     new int[]{r.id.name, r.id.numt, r.id.depart, r.id.destination, r.id.nbrpersonne}             );             list.setadapter(adapter);     }      public void getdata(){         class getdatajson extends asynctask<string, void, string>{              @override             protected string doinbackground(string... params) {                // json dowload web, not needed in question                return "";              }              @override             protected void onpostexecute(string result){                 myjson=result;                 showlist();             }         }         getdatajson g = new getdatajson();         g.execute();      }      public void goupdate(view v ) {         intent in = new intent(getbasecontext(), update.class);             in.putextra("numtelephone",((hashmap<string, string>)list.getadapter().getitem(0)).get("numtelephone"));         }          startactivity(in);     } } 

there simple , 1 solution. delete xml android:click property of list , add list#setonitemclicklistener in oncreate:

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_list_client);     list = (listview) findviewbyid(r.id.list);     personlist = new arraylist<hashmap<string,string>>();     getdata();      //this thing has been added     list.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             if(id!=-1){             intent in = new intent(getbasecontext(), update.class);             in.putextra("numtelephone", ((hashmap<string, string>) list.getadapter().getitem((int)id)).get("numtelephone")); //tag_numtelephone              startactivity(in);             }         }     }); } 

to move oncreate do:

   @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_list_client);         list = (listview) findviewbyid(r.id.list);         personlist = new arraylist<hashmap<string, string>>();         getdata();         list.setonitemclicklistener(new onmylistitemclicklistener());     }      context context = this;      private class onmylistitemclicklistener implements adapterview.onitemclicklistener {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             if(id!=-1){             intent in = new intent(context, update.class);             in.putextra("numtelephone", ((hashmap<string, string>) list.getadapter().getitem((int) id)).get("numtelephone")); //tag_numtelephone              startactivity(in);             }         }     } 

lesson learned - dont create sucha buttons - not aware in item are, cant extract id - instead use alertdialog

  public void onitemclick(adapterview<?> parent, view view, int position, long id) {         if(id!=-1){         alertdialog.builder al = new alertdialog.builder(context);         al.setmessage("are sure want it?");         al.setpositivebutton(android.r.string.ok, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialoginterface, int i) {                 intent in = new intent(context, update.class);                 in.putextra("numtelephone", ((hashmap<string, string>) list.getadapter().getitem((int) id)).get(tag_numtelephone));                  startactivity(in);             }         });         al.setnegativebutton(android.r.string.no, null);         al.show();          }             } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -