For Loop Until Button Is Pressed in Android - Stack Overflow PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

for loop until button is pressed in Android?


0

android

csv

I am new to Android. How can I run this For loop until the button is pressed? I am writing saved data of array into CSV file and storing it onto
SDcard. I want to keep the loop running until I press writeFileBtn and it saves the data into CSV.
@Override
public void onClick(View v) {

share

});

CSVWriter writer = null;


try
{
writer = new CSVWriter(new FileWriter(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "myfile.csv"), ',');
//String[] entries = "first#second#third".split("#"); // array of your values
writer.writeNext(entriesArr);
writer.close();
}
catch (IOException e)
{
Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_LONG).show();
}

improve this question

Muntazir Abbas
7 2

Asked
Dec 4 '15 at 16:16

Liuting
530 7 25

Edited
Dec 4 '15 at 16:49

short answer ... you need new Thread ... basically in run() of thread {while(running){ /*loop here*/} /* write file here*/ } ... then in on click of first button you starting
the thread ... and in second set running to false ... Selvin Dec 4 '15 at 16:19
of course it is just POC and have some caveats ... the real implementation should take to an account that activity can be closed ... or recreated ... so it would be
better to use fx service ..... etc. Selvin Dec 4 '15 at 16:25

add a comment

order by votes

1 Answer

You should use an AsyncTask to do this work on background.

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {


protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}

}
AsynkTask provides you a isCancelled() that can be used by your button.

share

improve this answer


josealfonsomora
51 4

Answered
Dec 4 '15 at 16:29

The problem is I am really new to this coding. I have tried many solutions but not successful so far. Can somebody help me in modification my code already
mentioned above? Muntazir Abbas Dec 4 '15 at 16:33
you mean: writing the code for you, for free? why? josealfonsomora's example should be enough Selvin Dec 4 '15 at 16:36

add a comment

Your Answer

log in
or
Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app

Post Your Answer

2016 Stack Exchange, Inc

You might also like