save user data during a day (the same day -> many

2019-01-28 00:21发布

I have an application where the user enters data in edittext and presses the save button.

By pressing 'save' I save in a file the user data (in one column) and the current date (in the other column).

Then , I press another button and make the plot (using achartengine) date (x axis) data (y axis).

So, entering data during a day ,results in saving for example: "1" (user data) -> 20/4/2013 , "2" -> 20/4/2013 , "3" -> 20/4/2013.

And in plot I have 3 points in y axis (ok) and 3 points in x axis (not ok).

I want to have one point in x axis because the data where entered in the same day.

I save data :

public void savefunc(){

        SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy"); 
        Date d=new Date();

        String formattedDate=thedate.format(d);
        Log.d("tag","format"+formattedDate);
        dates_Strings.add(formattedDate);


        double thedata=Double.parseDouble(value.getText().toString().trim());
            mydata.add(thedata);


        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard, "MyFiles");
        directory.mkdirs();            
        File file = new File(directory, filename);

        FileOutputStream fos;

        //saving them
        try {
           fos = new FileOutputStream(file);

              BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
              for (int i=0;i<mydata.size();i++){
                 bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
              }
              ...

How can I save the user data during a day ?

Maybe some check here : Date d=new Date(); ? To check if it is the same day.

Or here : bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");

But I can't figure.

For example I enter data " 1" , "2" ,"3" in date "20/4/2013".

This is what I get now using my code: This is what I get now http://i35.tinypic.com/2rmsck5.png

But i require graph like below: data entered on same day should be put together:: This is what I want http://i38.tinypic.com/255p5i9.png

---------------UPDATE--------------------------------------------------

  mRenderer.setXLabels(0);
    for (int i=0;i<mydata.size();i++){

        mRenderer.addXTextLabel(i,dates_Strings.get(i));

        Date lastDate=null;
        String lastdate="";

        try{

    // the initial date
Date initialDate=formatter.parse(dates_Strings.get(mydata.size()-1));

Calendar c = Calendar.getInstance();
c.setTime(initialDate);
c.add(Calendar.DATE, 1);  // increase date by one 
lastDate =c.getTime();                  

}catch ...
      }
    mRenderer.setXAxisMax(lastDate.getTime());
    mRenderer.addXTextLabel(i,dates_Strings.get(i));
    }

4条回答
We Are One
2楼-- · 2019-01-28 00:56

ok.

When you call new Date(), you also determine time of creation (default format is: January 1, 1970, 00:00:00 GMT). Because your points are created in different time but same date, your points are not aligned.

So you should do it like this:

Calendar thisDay = Calendar.getInstance();
thisDay.set(Calendar.HOUR, 0);
thisDay.set(Calendar.MINUTE, 0);
thisDay.set(Calendar.SECOND, 0);
Date d=thisDay.getTime();//this returns Date :) - it is funny but true

then you can use d as current date :).

Hope it is true and it helps, Toni

查看更多
Evening l夕情丶
3楼-- · 2019-01-28 01:06

There are a few possible solutions for this problem:

  1. instead of a date, put the unix time of the date (long value) . in order to show it, you can convert the unix time to formatted date .

  2. since excel can handle dates, edit the output file and use "=Date(year,month,day)" or "=DATEVALUE("2013/4/20")"

this is all because the problem isn't even related to android. it's about showing the data. the data is ok. it's just how you show it.

查看更多
聊天终结者
4楼-- · 2019-01-28 01:08

If I am not very much mistaken this is not a problem of saving or loading the data but simply of displaying the data. Your graph algorithm should recognize equal dates and do not make a new entry for it.

As it is, it seem like the date is treated as label, not as x-axis value, which would be reasonable because the date string is not numeric.

I suggest to check achartengine if there is a way to additional provide x-values and then let them only increase if the date string of the next entry is different of the previous entry.

You probably have to give a different model to achartengine.

I don't think it is a save problem because well the date stored is the right one, so any behavior there is mostly as expected.

查看更多
Juvenile、少年°
5楼-- · 2019-01-28 01:14

This is an AChartEngine issue indeed. The internal model used to be kept in ArrayLists and these issues didn't exist. At some point there was a community effort to make AChartEngine faster with a lot of data points. At that point, the model started to use a Map instead of an ArrayList. This implementation prevented having the same X value added several times. However, in order to fix this, I add a very small value to X, if it already exists. In your example, the first value is 20/04/2013 00:00:00.0, the second one is at 20/04/2013 00:00:00.001 and the third is 20/04/2013 00:00:00.002.

Now, the solution to your problem is to have a wider range on the X axis.

renderer.setXAxisMax(someDate.getTime());

where someDate can be something like 21/04/2013.

查看更多
登录 后发表回答