Putting a JSONArray into a list(Android) -
i have project can take jsonobject , set edittext i'm trying figure out how alter takes jsonarray , puts listview.
here current code:
public class js extends activity { private string url1 = "http://api.openweathermap.org/data/2.5/weather?q=chicago"; //private string url1 = "http://bisonsoftware.us/hhs/messages.json"; private textview temperature;//,country,temperature,humidity,pressure; private handlejson obj; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_js); //location = (edittext)findviewbyid(r.id.edittext1); //country = (textview)findviewbyid(r.id.edittext2); temperature = (textview)findviewbyid(r.id.edittext3); //humidity = (edittext)findviewbyid(r.id.edittext4); //pressure = (edittext)findviewbyid(r.id.edittext5); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items //to action bar if present. getmenuinflater().inflate(r.menu.js, menu); homecoming true; } public void open(view view){ //string url = location.gettext().tostring(); //string finalurl = url1 + url; //country.settext(url1); obj = new handlejson(url1); obj.fetchjson(); while(obj.parsingcomplete); //country.settext(obj.getcountry()); temperature.settext(obj.gettemperature()); //humidity.settext(obj.gethumidity()); //pressure.settext(obj.getpressure()); } } public class handlejson { //private string country = "temperature"; private string temperature = "clouds"; //private string humidity = "humidity"; //private string pressure level = "pressure"; private string urlstring = null; public volatile boolean parsingcomplete = true; public handlejson(string url){ this.urlstring = url; } /*public string getcountry(){ homecoming country; }*/ public string gettemperature(){ homecoming temperature; } /*public string gethumidity(){ homecoming humidity; } public string getpressure(){ homecoming pressure; }*/ @suppresslint("newapi") public void readandparsejson(string in) { seek { jsonobject reader = new jsonobject(in); //jsonobject sys = reader.getjsonobject("main"); //country = sys.getstring("temp"); jsonobject main = reader.getjsonobject("clouds"); temperature = main.getstring("all"); //pressure = main.getstring("pressure"); //humidity = main.getstring("humidity"); parsingcomplete = false; } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } } public void fetchjson(){ thread thread = new thread(new runnable(){ @override public void run() { seek { url url = new url(urlstring); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(10000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); // starts query conn.connect(); inputstream stream = conn.getinputstream(); string info = convertstreamtostring(stream); readandparsejson(data); stream.close(); } grab (exception e) { e.printstacktrace(); } } }); thread.start(); } static string convertstreamtostring(java.io.inputstream is) { java.util.scanner s = new java.util.scanner(is).usedelimiter("\\a"); homecoming s.hasnext() ? s.next() : ""; } }
i have been trying figure out while can't find way implement through way i'm parsing data. in advance help given.
here json:
"messages":["this demo message. enjoy!","another demonstration message stored in json format.","json stands javascript object notation (i think)"]
what you're asking several questions. break downwards yourself, , think you'll have much easier time.
create functionality executes net service request , returns response, handling error cases, etc.
create "weather" class reflects contents of json (e.g. yours, class temperature, pressure, humidity, etc.)
create functionality checks response validity , constructs weather object it.
create collection of these weather objects (list, set, etc.) response
create custom listadapter takes instance of weather object , translates ui.
???
profit
taken individually, you'll have much easier time tackling this. custom adapter simple implement. so, have simple weather class this:
public final class weather { public final string temperature; public final string pressure; public final string humidity; public weather(string temperature, string pressure, string humidity) { this.temperature = temperature; this.pressure = pressure; this.humidity = humidity; } public static weather valueof(jsonobject json) throws jsonexception { string temperature = json.getstring("temp"); string pressure level = json.getstring("pressure"); string humidity = json.getstring("humidity"); } }
make simple subclass of baseadapter
takes weather
, adapts custom layout you've created:
public final class weatheradapter extends baseadapter { private final list<weather> mweatherlist; private final layoutinflater minflater; public weatheradapter(context ctx, collection<weather> weather) { minflater = layoutinflater.from(ctx); mweatherlist = new arraylist<>(); mweatherlist.addall(weather); } @override public int getcount() { // homecoming size of info set homecoming mweatherlist.size(); } @override public weather getitem(int position) { // homecoming item in our info set @ given position homecoming mweatherlist.get(position); } @override public long getitemid(int position) { // not useful in our case; homecoming position homecoming position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { // there's no view re-use, inflate new one. // assumes you've created layout "weather_list_item.xml" // textviews pressure, temperature, , humidity convertview = minflater.inflate(r.layout.weather_list_item, parent, false); // cache views findviewbyid() efficiency convertview.settag(new weatherviewholder(convertview)); } // weather item list position weather weather = getitem(position); weatherviewholder holder = (weatherviewholder) convertview.gettag(); // assign text, icons, etc. layout holder.pressure.settext(weather.pressure); holder.temperature.settext(weather.temperature); holder.humidity.settext(weather.humidity); homecoming convertview; } public static class weatherviewholder { public final textview pressure; public final textview humidity; public final textview temperature; public weatherviewholder(view v) { pressure level = (textview) v.findviewbyid(r.id.pressure); humidity = (textview) v.findviewbyid(r.id.humidity); temperature = (textview) v.findviewbyid(r.id.temperature); } } }
android android-json
No comments:
Post a Comment