android - Call AsyncTask in a Fragment -
i have class extends fragment. call asynctask class in fragment. doesn't obtain error in mylogcat doesn't display null in listview. below paste code use, code more simple, read json file , insert in custom listview. code works if change class in activity, want transform activity in fragment.
thanks in advance.
public class myfragment extends fragment { jsonparser jsonparser = new jsonparser(); arraylist<hashmap<string, string>> unilist; jsonarray articles = null; listview list; listuniadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); new loadnews().execute(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { linearlayout ll = (linearlayout)inflater.inflate(r.layout.uni_list, container, false); list = (listview) ll.findviewbyid(android.r.id.list); return ll; } class loadnews extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); } protected string doinbackground(string... args) { list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("s", "s")); string json = jsonparser.makehttprequest(variable.url_universitometro, "get", params); try { unilist = new arraylist<hashmap<string, string>>(); articles = new jsonarray(json); if (articles != null) { (int = 0; < articles.length(); i++) { jsonobject c = articles.getjsonobject(i); string title = c.getstring(variable.uni_name); string voto = c.getstring(variable.uni_voto); hashmap<string, string> map = new hashmap<string, string>(); map.put(variable.uni_name, title); map.put(variable.uni_voto, voto); unilist.add(map); if (iscancelled()) break; } } else { log.d("articles: ", "null"); } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { list.setscrollingcacheenabled(false); adapter = new listuniadapter(getactivity(), unilist); list.setadapter(adapter); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view view, int arg2, long arg3) { } }); } }
}
Comments
Post a Comment