https://cloud.githubusercontent.com/assets/228704/23704199/b79d14ba-03d1-11e7-85e4-350c48be11f6.png
i want take out of things in picture crossed out, here code below, thank you.
package pkc.trafficquest.sccapstone.trafficquest; import android.content.intent; import android.net.uri; import android.os.bundle; import android.os.environment; import android.support.v7.app.appcompatactivity; import android.view.menu; import android.view.menuitem; import android.widget.arrayadapter; import android.widget.listadapter; import android.widget.listview; import android.widget.toast; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; public class logactivity extends appcompatactivity { private arraylist<string> accidentlist; // used string version of accident list private arraylist<accidents> accidents; // used list of type accidents private listview listview; private string csvstring; public static final int request_code_main = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_log); intent logintent = getintent(); bundle data = logintent.getextras(); // checks see if there data intent, if there is, instantiate list of names , set list view if (logintent.getextras() != null){ accidentlist = logintent.getstringarraylistextra("accidentlist"); accidents = data.getparcelablearraylist("logaccidentlist"); listadapter accadapter = new arrayadapter<string>(this, r.layout.accident_list, accidentlist); listview = (listview) findviewbyid(r.id.alistview); listview.setadapter(accadapter); csvstring = createcsv(accidents); // makes csv out of list of accidents } /* // attention: auto-generated implement app indexing api. // see https://g.co/appindexing/androidstudio more information. client = new googleapiclient.builder(this).addapi(appindex.api).build();*/ } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.log_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // menu select between downloading or emailing csv of requested accidents int id = item.getitemid(); if (id == r.id.action_download){ // downloads csv of requested accidents if selected savecsv(csvstring); } else if (id == r.id.action_email){ // downloads , emails csv of requested accidents if selected sendemail(csvstring); } return super.onoptionsitemselected(item); } /* creates csv file list of requested accidents @param list of accidents create csv @return csv of requested accidents */ public string createcsv(arraylist<accidents> acclist){ string csv = ""; (int i=0; i<acclist.size(); i++){ // loop through accidents , add each detail csv accidents accident = acclist.get(i); // individual accident csv += "\"" + accident.getpoint().getcoordinates().get(0) + ", " + accident.getpoint().getcoordinates().get(1) + "\"," + // add latitude , longitude, separated space list "\"" + accident.gettopoint().getcoordinates().get(0) + ", " + accident.gettopoint().getcoordinates().get(1) + "\"," + // add topoint(where accident ends) list, latitude , longitude separated space accident.getdescription() + "," + // add accident description list accident.getroadclosed() + "," + // add if road closed (boolean value) interpretseverity(accident) + "," + // add severity of accident list interprettime(accident.getstart()) + "," + // add start time of accident list interprettime(accident.getend()) + "," + // add end time of accident list interprettype2(accident) + "\n"; // add type of accident list (accident, weather, hazard, etc.) , go next line string starttime = "start time: 03/08/2017";//interprettime(accident.getstart()); string starttimewithouttitle= starttime.substring(starttime.indexof(":")+1,starttime.length()); string severity = "severity: minor"; string severitywithouttitle= severity.substring(severity.indexof(":")+1,severity.length()); string cvs=starttimewithouttitle+","+severitywithouttitle; system.out.println(cvs); //just testing } return csv; // return string in csv format } /* write file internal storage @param data string data write */ public void savecsv(string data) { file file = null; file root = environment.getexternalstoragedirectory(); // path of root directory if (root.canwrite()) { // checks if application can modify path file dir = new file(root.getabsolutepath() + "/accidentdata"); // new directory dir.mkdirs(); file = new file(dir, "data.csv"); // file wrote fileoutputstream out = null; try { out = new fileoutputstream(file); // output stream file location } catch (filenotfoundexception e) { e.printstacktrace(); } try { out.write(data.getbytes()); // write encoded string file } catch (ioexception e) { e.printstacktrace(); } try { out.close(); // close output stream } catch (ioexception e) { e.printstacktrace(); } toast.maketext(getapplicationcontext(), "downloaded.", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "file failed write.", toast.length_short).show(); // prints message if file not write file } } /* interprets time human readable format @param t string accident list interpret @return converted time */ public string interprettime (string t) { string timestring = t.substring(6, t.length()-2); // gets rid of leading , trailing slashes , parenthesis string date; // value return long time = long.parselong(timestring); // parse string long simpledateformat sdf = new simpledateformat("mm/dd/yyyy 'at' hh:mm z"); // sets format date = sdf.format(new date(time)); // sets entered string simpledateformat return date; // return date } /* interprets severity codes received request @param acc accident severity data @return interpreted severity code */ public string interpretseverity (accidents acc) { int severity = acc.getseverity(); // type code accident string sevstring; // value return switch (severity) { case 1: sevstring = "low impact"; break; case 2: sevstring = "minor"; break; case 3: sevstring = "moderate"; break; case 4: sevstring = "serious"; break; default: sevstring = "incorrect value"; break; } return sevstring; // return severity code } /* interprets each type code means @param acc accidents object type code @return interpreted type of accident */ public string interprettype2(accidents acc){ int type = acc.gettype2(); // type code accident string typestring; // value return switch (type) { case 1: typestring = "accident"; break; case 2: typestring = "congestion"; break; case 3: typestring = "disabled vehicle"; break; case 4: typestring = "mass transit"; break; case 5: typestring = "miscellaneous"; break; case 6: typestring = "other news"; break; case 7: typestring = "planned event"; break; case 8: typestring = "road hazard"; break; case 9: typestring = "construction"; break; case 10: typestring = "alert"; break; case 11: typestring = "weather"; break; default: typestring = "incorrect value"; break; } return typestring; } /* create email intent, , send requested csv file after creates it, email intent. @param data string data send csv */ public void sendemail(string data) { file file = null; file root = environment.getexternalstoragedirectory(); // path of root directory if (root.canwrite()) { // checks if application can modify path file dir = new file(root.getabsolutepath() + "/accidentdata"); // new directory dir.mkdirs(); file = new file(dir, "data.csv"); // file wrote fileoutputstream out = null; try { out = new fileoutputstream(file); // output stream file location } catch (filenotfoundexception e) { e.printstacktrace(); } try { out.write(data.getbytes()); // write encoded string file } catch (ioexception e) { e.printstacktrace(); } try { out.close(); // close output stream } catch (ioexception e) { e.printstacktrace(); } } else { toast.maketext(getapplicationcontext(), "file failed write.", toast.length_short).show(); // prints message if file not write file } uri u = null; u = uri.fromfile(file); // contents of file intent emailintent = new intent(intent.action_send); // create intent send csv emailintent.putextra(intent.extra_subject, "trafficquest: csv"); // subject of email emailintent.putextra(intent.extra_text, "here csv, requested."); // body of email emailintent.putextra(intent.extra_stream, u); // add attachment csv emailintent.settype("text/plain"); // sets type plain, supports csv files startactivity(intent.createchooser(emailintent, "send email with:")); } public void toastmaker(string toast) { toast.maketext(getapplicationcontext(), toast, toast.length_short).show(); }
}
i try this:
string data=interprettime(accident.getstart()); string[] parts = data.split(":"); string part1 = parts[0]; //contains title string part2 = parts[1]; //your data
the idea split string using ":" separator , somehow save parts need.
other way using part of code assuming interprettime(accident.getstart()) returning string:
string starttime = interprettime(accident.getstart()); string starttimewithouttitle= starttime.substring(starttime.indexof(":")+1,starttime.length);
here can read how substring method works substring(int,int)
Comments
Post a Comment