java - Unable to execute Asynctask inside the setOnClickListener -
i'm having error on line -> new gethttpresponse(this).execute();
it doens't have error when put outside button
i got code http://www.android-examples.com/create-dynamic-listview-using-json-parsing-php-mysql/ , added button can refreshed when database updated
public class mainactivity extends activity { listview listcollege; progressbar procollagelist; button bton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); bton = (button) findviewbyid(r.id.button); listcollege = (listview)findviewbyid(r.id.listcollege); procollagelist = (progressbar)findviewbyid(r.id.procollagelist); new gethttpresponse(this).execute(); bton.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { toast.maketext(mainactivity.this, "list refreshed", toast.length_short).show(); new gethttpresponse(this).execute(); }}); } private class gethttpresponse extends asynctask<void, void, void> { private context context; string result; list<benedict.com.listviewpractice.cources> collegelist; public gethttpresponse(context context) { this.context = context; } @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... arg0) { httpservice httpservice = new httpservice("http://10.0.2.2/courses.php"); try { httpservice.executepostrequest(); if(httpservice.getresponsecode() == 200) { result = httpservice.getresponse(); log.d("result", result); if(result != null) { jsonarray jsonarray = null; try { jsonarray = new jsonarray(result); jsonobject object; jsonarray array; benedict.com.listviewpractice.cources college; collegelist = new arraylist<benedict.com.listviewpractice.cources>(); for(int i=0; i<jsonarray.length(); i++) { college = new benedict.com.listviewpractice.cources(); object = jsonarray.getjsonobject(i); college.cources_name = object.getstring("cource_name"); college.cources_description = object.getstring("cources_description"); collegelist.add(college); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } } else { toast.maketext(context, httpservice.geterrormessage(), toast.length_short).show(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { procollagelist.setvisibility(view.gone); listcollege.setvisibility(view.visible); if(collegelist != null) { listadapter adapter = new listadapter(collegelist, context); listcollege.setadapter(adapter); } } } }
inside onclicklistener, this
represents instance of onclicklistener, not context object expected, this
should mainactivity.this
.
new gethttpresponse(mainactivity.this).execute();
Comments
Post a Comment