android - How to: Create an onclick listener for ListView -
i've used listview show data wamp server using mysql, using json parsing , following code....
public class jsontask extends asynctask<string, string, list<pumpmodel>> { @override protected list<pumpmodel> doinbackground(string... params) { httpurlconnection connection = null; bufferedreader reader = null; try { url url = new url(params[0]); connection = (httpurlconnection) url.openconnection(); connection.connect(); inputstream stream = connection.getinputstream(); reader = new bufferedreader(new inputstreamreader(stream)); stringbuffer buffer = new stringbuffer(); string line = ""; while ((line = reader.readline()) != null) { buffer.append(line); } string finaljson = buffer.tostring(); jsonobject parentobject = new jsonobject(finaljson); jsonarray parentarray = parentobject.getjsonarray("server_response"); list<pumpmodel> pumpmodellist = new arraylist<>(); (int = 0; < parentarray.length(); i++) { jsonobject finalobject = parentarray.getjsonobject(i); pumpmodel pumpmodel = new pumpmodel(); pumpmodel.setpump(finalobject.getstring("pump")); pumpmodel.setavailable(finalobject.getstring("available")); pumpmodellist.add(pumpmodel); } return pumpmodellist; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } { if (connection != null) { connection.disconnect(); } try { if (reader != null) { reader.close(); } } catch (ioexception e) { e.printstacktrace(); } } return null; } @override protected void onpostexecute(list<pumpmodel> result) { super.onpostexecute(result); //todo need set data list pumpadapter adapter = new pumpadapter(getapplicationcontext(), r.layout.row, result); lvpump.setadapter(adapter); } } public class pumpadapter extends arrayadapter { private list<pumpmodel> pumpmodellist; private int resource; private layoutinflater inflater; public pumpadapter(context context, int resource, list<pumpmodel> objects) { super(context, resource, objects); pumpmodellist = objects; this.resource = resource; inflater = (layoutinflater) getsystemservice(layout_inflater_service); } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = inflater.inflate(resource, null); } imageview ivicon; textview tvpump; ivicon = (imageview) convertview.findviewbyid(r.id.ivicon); tvpump = (textview) convertview.findviewbyid(r.id.tvpump); // later, when want display image imageloader.getinstance().displayimage(pumpmodellist.get(position).getavailable(), ivicon); // default options used tvpump.settext(pumpmodellist.get(position).getpump()); return convertview; } }
now want create onclick listener list view, can used open activity, , show description of pumps in textview.
in activity, defined listview
you can write
listview.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?>adapter,view v, int position){ itemclicked item = adapter.getitematposition(position); intent intent = new intent(activity.this,destinationactivity.class); //based on item add info intent startactivity(intent); } }); public itemclicked getitem(int position){ return items.get(position); }
Comments
Post a Comment