sorting - Android: sort by date in the JSON data -
i new android. i'm trying sort date in json data, nothing works. i'm not getting error. i've tried many different ways, not working.
i did lot of searching not figure out how implement this. how can sort days column? thank in advance.
here's code
public class parsejsontask extends asynctask< void , void , void > { public handler handler = new handler(); public activity act = null; private static string tag_services = "services"; private static string tag_id = "id"; private static string tag_command = "command"; private static string tag_days = "days"; private static string tag_hours = "hours"; private static string tag_osms = "osms"; private static string tag_isms = "isms"; private static string tag_timeout = "timeout"; public string sms_sent = "sms gönderildi"; public string sms_delivered = "sms İletildi"; public string servicestring = ""; arraylist<servicedata> services; @override public void onpreexecute() { super.onpreexecute(); services = new arraylist<servicedata>(); } @override public void doinbackground(void... params) { webservicehandler webservicehandler = new webservicehandler(); string jsonstr = webservicehandler.getjsondata("http://jsonblob.com/55e34310e4b01190df36e861"); try { jsonobject jsonobject = new jsonobject(jsonstr); final jsonarray contactsjson = jsonobject.getjsonarray(tag_services); (int = 0; < contactsjson.length(); i++) { servicedata aservicedata = new servicedata(); //json parse istedimiz veriyi kullanabiliriz. jsonobject serviceobject = contactsjson.getjsonobject(i); aservicedata.id = serviceobject.getstring(tag_id); aservicedata.command = serviceobject.getstring(tag_command); aservicedata.days = serviceobject.getstring(tag_days); aservicedata.hours = serviceobject.getstring(tag_hours); aservicedata.osms = serviceobject.getstring(tag_osms); aservicedata.isms = serviceobject.getstring(tag_isms); aservicedata.timeout = serviceobject.getstring(tag_timeout); string input = aservicedata.days + " " + aservicedata.hours; date date = new simpledateformat("yyyy-mm-dd hh:mm").parse(input); long milliseconds = date.gettime(); final long millisecondsfromnow = milliseconds - (new date()).gettime(); aservicedata.milliseconds = milliseconds; services.add(aservicedata); if(millisecondsfromnow > 0) { new datesendsms().oncreate(aservicedata.days, aservicedata.hours, aservicedata.osms, aservicedata.command); thread.sleep(integer.parseint(aservicedata.timeout) * 60000); } //timeout aşağı kısımda sürelendirilecek } } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); string servicestring = ""; (servicedata aservicedata:services){ servicestring+=aservicedata.tostring(); } collections.sort(services, new comparator<servicedata>() { @override public int compare(servicedata t1, servicedata t2) { return t1.milliseconds <= t2.milliseconds ? -1 : 1; } }); // here sorted data (servicedata aservicedata : services) { // move datesendsms here. above can add additional logic millis new datesendsms().oncreate(aservicedata.days, aservicedata.hours, aservicedata.osms, aservicedata.command); log.d("+++++", aservicedata.tostring()); } } }
servicedata class:
public static class servicedata { public long milliseconds; public string id = ""; public string command = ""; public string days = ""; public string hours = ""; public string osms = ""; public string isms = ""; public string timeout = ""; @override public string tostring() { return id + ", " + command + ", " + days + ", " + hours + ", " + osms + ", " + isms + ", " + timeout + "\n \n "; } }
add time field servicedata
class
servicedate { ... long milliseconds; ... }
fill field in loop:
long milliseconds = date.gettime(); aservicedata.milliseconds = milliseconds;
sort services
in onpostexecute
collections.sort(services, new comparator<servicedata>() { @override public int compare(servicedata t1, servicedata t2) { return t1.milliseconds <= t2.milliseconds ? -1 : 1; } });
Comments
Post a Comment