My problem is that the file is empty after clicking stop button but only when the time changes e.g. when i click start button at 16:49:39 and then stop button at 16:49:57 then the file is empty. But when I click stop button 16:50:01 then the file saves with data.Please tell my why this happens ? And how to solve this. Below you can see the code from my application:
OnClick code:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startButton:
stoper.start();
try {
writer = new CSVwriter(dateFormat.format(date));
} catch (IOException e) {
e.printStackTrace();
}
handler.postDelayed(UpdateView, 120);
break;
case R.id.stopButton:
stoper.stop();
try {
writer.closeFile();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
private Runnable UpdateView = new Runnable() {
public void run() {
timerText.setText(stoper.toString());
pulsText.setText(""+accelerationX);
try {
writer.insertLine(new DataObject(stoper.toString(), accelerationX, accelerationY, accelerationZ));
} catch (IOException e) {
e.printStackTrace();
}
handler.postDelayed(UpdateView, 100);
}
};
OnCreate code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View startBtn = findViewById(R.id.startButton);
startBtn.setOnClickListener(this);
View stopBtn = findViewById(R.id.stopButton);
stopBtn.setOnClickListener(this);
handler = new Handler();
}
Have you tried manually calling flush() if the writer has a flush() method? (Most file writing classes should have a flush() method unless CSVwriter is your own class.)
Manually flushing the stream may help.
EDIT: As a side note, does the stoper.stop() ever throw an error? If it does move it to after the file close in the finally block to ensure it is always called after the file has been flushed and closed.