android JSON in ListFragment -
i'm new android development. i've no problem json when used in mainactivity got problem when move code listfragment
this line nothing happen: jsonobject json = userfunctions.announcementfeed(myid, mytype);
here full code:
package com.example.demo123; import java.util.arraylist; import java.util.hashmap; import library.userfunctions; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.os.bundle; import android.support.v4.app.listfragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.simpleadapter; public class announcementlist extends listfragment { private static final string tag = "myannouncement"; jsonarray announcementlist = null; userfunctions userfunctions; string myid = mainactivity.myid; string mytype = mainactivity.mytype; @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { return super.oncreateview(inflater, container, savedinstancestate); } @override public void onactivitycreated(bundle savedinstancestate) { log.i(tag,"start announcement list"); arraylist<hashmap<string, string>> theannnouncement = new arraylist<hashmap<string, string>>(); jsonobject json = userfunctions.announcementfeed(myid, mytype); try { if (json.getstring("success") != null) { announcementlist = json.getjsonarray("announcedt"); (int = 0; < announcementlist.length(); i++) { jsonobject c = announcementlist.getjsonobject(i); string title = c.getstring("topic"); string course = c.getstring("course") + " - " + c.getstring("coursetitle"); string created = "by " + c.getstring("entered_by") + " | " + c.getstring("entry_date"); hashmap<string, string> map = new hashmap<string, string>(); map.put("title", title); map.put("course", course); map.put("created", created); theannnouncement.add(map); } } } catch (jsonexception e) { e.printstacktrace(); } string[] = { "title","course","created" }; int[] = { r.id.title,r.id.course,r.id.created}; simpleadapter adapter = new simpleadapter(getactivity().getbasecontext(), theannnouncement, r.layout.list_item, from, to); setlistadapter(adapter); }
}
why absolutely necessary put logic in onactivitycreated? fragment not initialized @ point might want move logic onresume or somewhere else.
edit: took @ code linked, announcementfeed() method.
it looks you're trying networking on main thread, method;
public jsonobject announcementfeed(string username, string usertype){ .................................. jsonobject json = jsonparser.getjsonfromurl(loginurl, params); // return json return json; }
you're calling method in onactivitycreated. suggest moving background thread or asynctask. still, without logcat can't 100% sure what's happening, @ least we're able point out off top.
Comments
Post a Comment