Android how to parse json into listtview inside a class that extends fragment -
i trying fragment there listview inside , trying populate listview using json. have 1 error , dont know put single error have. error says unreachable statement, when put getjson() below } says invalid method declaration
here code listview inside fragment. there error pointing @ getjson(); below rootview. thanks
public class news extends fragment { private listview lv; private string json_string; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view rootview = inflater.inflate(news, container, false); lv = (listview) rootview.findviewbyid(r.id.listview3); return rootview; getjson(); } private void showresult(){ jsonobject jsonobject = null; arraylist<hashmap<string,string>> list = new arraylist<>(); try { jsonobject = new jsonobject(json_string); jsonarray result = jsonobject.getjsonarray(config.tag_json_array1); for(int = 0; i<result.length(); i++){ jsonobject jo = result.getjsonobject(i); string nid = jo.getstring(config.tag_nid); string title = jo.getstring(config.tag_title); string content = jo.getstring(config.tag_content); string n_date = jo.getstring(config.tag_n_date); hashmap<string,string> match = new hashmap<>(); match.put(config.tag_nid, nid); match.put(config.tag_title,title); match.put(config.tag_content,content); match.put(config.tag_n_date,n_date); list.add(match); } } catch (jsonexception e) { e.printstacktrace(); } listadapter adapter = new simpleadapter( getactivity(), list, r.layout.newsadapterlayout, new string[]{config.tag_title,config.tag_content, config.tag_n_date, config.tag_nid}, new int[]{ r.id.title, r.id.content, r.id.n_date}); lv.setadapter(adapter); } private void getjson(){ class getjson extends asynctask<void,void,string> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onpostexecute(string s) { super.onpostexecute(s); json_string = s; showresult(); } @override protected string doinbackground(void... params) { requesthandler rh = new requesthandler(); string s = rh.sendgetrequest(config.url_news); return s; } } getjson gj = new getjson(); gj.execute(); } }
your getjson()
call after return
statement, not possible ever executed. "unreachable statement" error means.
you move getjson()
call line before return
statement , should solve problem. without running it's hard know if there other problems, @ least 1 should solved.
Comments
Post a Comment