solved: edited: solved simple code doesnt need custom adapter class. solved in single class. hope beneficial one.
public class newsfetch extends sherlockfragment { string myjson; private static final string tag_results="result"; string id; private static final string tag_name = "title"; private static final string tag_add =html.fromhtml("content").tostring(); jsonarray peoples = null; arraylist<hashmap<string, string>> personlist; listview liste; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.news_fetch, container, false); liste = (listview)rootview.findviewbyid(r.id.listviews); personlist = new arraylist<hashmap<string,string>>(); getdata(); // id = getactivity().getintent().getextras().getstring("id"); liste.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { // on selecting single news song information toast.maketext(getactivity().getapplicationcontext(), "i clicked", toast.length_short).show(); } }); return rootview; } protected void showlist(){ try { jsonobject jsonobj = new jsonobject(myjson); peoples = jsonobj.getjsonarray(tag_results); (int i=0;i<peoples.length();i++){ jsonobject c = peoples.getjsonobject(i); string titles = c.getstring(tag_name); string address = c.getstring(tag_add); hashmap<string,string> persons = new hashmap<string,string>(); persons.put(tag_name,titles); persons.put(tag_add,address); personlist.add(persons); } listadapter adapter = new simpleadapter( getactivity().getapplicationcontext(), personlist, r.layout.news_list, new string[]{tag_name,tag_add}, new int[]{ r.id.titles, r.id.cont} ); liste.setadapter(adapter); } catch (jsonexception e) { e.printstacktrace(); } } public void getdata(){ class getdatajson extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams()); httppost httppost = new httppost("http://10.0.2.2/json/news.php"); html.fromhtml(tag_name).tostring(); // depends on web service httppost.setheader("content-type", "application/json"); inputstream inputstream = null; string result = null; try { httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); inputstream = entity.getcontent(); // json utf-8 default bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } result = sb.tostring(); } catch (exception e) { // oops } { try{if(inputstream != null)inputstream.close();}catch(exception squish){} } return result; } @override protected void onpostexecute(string result){ myjson=result; showlist(); html.fromhtml(tag_name).tostring(); } } getdatajson g = new getdatajson(); g.execute(); } } problem : have 3 menus in navigation drawer. when use same code first menu option (json location , strings different) worked fine when tried use following code second menu option(with different json location , string name) doesn't show blank activity opens , shows nothing. have used internet permission in manifest.xml . appreciated. in advance
public class newnewsmain extends sherlockfragment { private listview listedview; arraylist<newnews> newslist; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_newnews, container, false); listedview = (listview) rootview.findviewbyid(r.id.listed); newslist= new arraylist<>(); new newsasynctask().execute("http://www.thebritishcollege.edu.np/api/news"); return rootview; } public class newsasynctask extends asynctask<string,void,boolean> { @override protected boolean doinbackground(string... params) { try { httpclient client = new defaulthttpclient(); httppost post = new httppost(params[0]); httpresponse response = client.execute(post); int status = response.getstatusline().getstatuscode(); if (status == 200) { httpentity entities = response.getentity(); string datas = entityutils.tostring(entities); jsonobject jobje = new jsonobject(datas); jsonarray jarrays = jobje.getjsonarray("result"); (int = 0; < jarrays.length(); i++) { newnews newss = new newnews(); jsonobject jrealsobject = jarrays.getjsonobject(i); newss.settitle(jrealsobject.getstring("title")); // news.setthumbnail(jrealsobject.getstring("thumbnail")); newslist.add(newss); } return true; } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return false; } @override protected void onpostexecute(boolean result) { super.onpostexecute(result); if(result==true){ newnewsadapter adapters= new newnewsadapter(getactivity().getapplicationcontext(), r.layout.newnews, newslist); listedview.setadapter(adapters); } } }
Comments
Post a Comment