我有一个活动,我从编辑文本中获取输入并将其存储在列表中. 我还在列表中存储当前日期. 然后,按下保存按钮,保存上面的内容. 第二天,用户输入更多数据并保存,依此类推. 我想用x轴日
我还在列表中存储当前日期.
然后,按下保存按钮,保存上面的内容.
第二天,用户输入更多数据并保存,依此类推.
我想用x轴日期格式制作一个图,用y轴制作用户输入的值.
在一项活动中,我有:
... String filename = "data.csv"; List<Double> mydata=new ArrayList<Double>(); List<Date> mydate=new ArrayList<Date>(); ....value=(EditText) findViewById(R.id.enter_data); ... switch (v.getId()){ case R.id.savebtn: savefunc(); break; case R.id.graphicsbtn: Intent i = new Intent(); i.setClassName(this,LineGraph.class.getName()); this.startActivity(i); break; public void savefunc(){ SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); Date d=new Date(); try{ d=thedate.parse(filename); mydate.add(d); } catch (ParseException e){ // TODO Auto-generated catch block e.printStackTrace(); } double thedata=Double.parseDouble(value.getText().toString().trim()); mydata.add(thedata); .. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); for (int i=0;i<mydate.size();i++){ bw.write(mydate.get(i)+","+mydata.get(i)+"\n"); ...
在LineGraph活动中:
public class LineGraph extends Activity { private static List<Date> date = new ArrayList<Date>(); private static List<Double> data = new ArrayList<Double>(); public Intent getIntent(Context context){ readfunc(); TimeSeries series = new TimeSeries("Showing data"); for (int i=0;i<date.size();i++){ series.add(date.get(i),data.get(i)); }
读取功能:
public void readfunc(){ SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); Date d=new Date(); try{ d=thedate.parse(filename); } catch.. BufferedReader br = new BufferedReader(new InputStreamReader(fis)); do { s = br.readLine(); if (s != null ){ String[] splitLine = s.split(","); date.add(d);//Double.parseDouble(splitLine[0])); data.add(Double.parseDouble(splitLine[1]));
我有这些问题:
1)我收到的文件是空的(Date的一些问题,因为保存和读取文件的方法有效).
2)在图形屏幕出现一个白色背景(当然没有数据,因为文件是空的),但为什么是白色背景?我使用相同的代码用于其他目的,我没有收到白色背景.
3)我不知道如何在x轴上使用日期.我应该使用List吗?清单? .
———————— UPDATE ————————- ——————————–
好的,终于!(在用户’丹’建议之后)
我使用ChartFactory.getTimeChartView(this,dataset,mRenderer,“dd / MM / yyyy”);
而不是ChartFactory.getLineChartIntent(context,dataset,mRenderer,“dd / MM / yyyy”);
并且您不需要使用字符串列表,只需使用日期列表
处理您文件的代码必须是这样的(未编译):public void savefunc(){ List<String> myDate = new ArrayList<String>(); //To store the formatted dates SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); Date d=new Date(); //the current date String sd = thedate.format(d); // sd contains "16/04/2013", the formatted date myDate.add(sd); double thedata=Double.parseDouble(value.getText().toString().trim()); mydata.add(thedata); ... BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); for (int i=0;i<mydate.size();i++){ bw.write(mydate.get(i)+","+mydata.get(i)+"\n"); } } public void readfunc(){ SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); Date d; BufferedReader br = new BufferedReader(new InputStreamReader(fis)); do { s = br.readLine(); if (s != null ){ String[] splitLine = s.split(","); //first substring is the formatted date date.add(thedate.parse(splitLine[0])); //do something with exception data.add(Double.parseDouble(splitLine[1])); ...
希望能帮助到你.