0

I'm very new to programming. I have a program that has lots and lots of methods that run consecutively to each other. My application freezes until its done calculating. I want to put everything in AsyncTask but I'm having a really hard time understanding how it works.

This is what I have so far as a separate java file.

import android.os.AsyncTask;

public class ConvertToStringToIntt extends AsyncTask<String, Void, Integer> {

    @Override
    protected Integer doInBackground(String... timeString) {
        String time = timeString;
        time = time.replace(":","");
        int value = -2;
        try {
            value = Integer.parseInt(time);
        } catch (NumberFormatException e) {
            Message.message(this, "String time could not be converted to int");
        }
        return value;
    }
}

This method basically takes in a String of time and returns it as an int. But I am getting an error in String time = timeString;

says:

Incompatible types Required: java.lang.String Found: java.lang.String[]

I don't know what that means. Any help would be great, and thank you.

T-Heron
  • 5,385
  • 7
  • 26
  • 52
Ricardo Carballo
  • 125
  • 1
  • 10
  • 2
    timeString is an Array of String, if you send 1 string upon executing the AsyncTask, then use String time = timeString[0]; – Komang Sidhi Artha Mar 21 '17 at 02:33
  • See http://stackoverflow.com/questions/3158730/java-3-dots-in-parameters for an explanation of the `String...` syntax. – GertG Mar 21 '17 at 10:40

1 Answers1

0

With your approach, and if you are sure of passing just a string //irrespective of business logic

public class ConvertToStringToIntt extends AsyncTask<String, Void, Integer> {

    @Override
    protected Integer doInBackground(String... timeString) {
        String time = timeString[0];
        time = time.replace(":","");
        int value = -2;
        try {
            value = Integer.parseInt(time);
        } catch (NumberFormatException e) {
            Message.message(this, "String time could not be converted to int");
        }
        return value;
    }
}
  • I get it doInBackground sets arrays, that worked. The int value will always return a simple int. I currently access the AsyncTask in the MainActivity, how can I get that int into a variable in the MainActivity. ConvertToString convertToString = new ConvertToString(); int i = convertToString.execute(getTimeString); does not work. – Ricardo Carballo Mar 21 '17 at 15:28