1

How do I pass value on doInBackground so It can use it. When I type the String directly inside doInBackgroud it works. But I have three type "Professional", "General", "Special" I don't want to create 3 asyncTask. In my research i found that I need to create contructor but im don't get It.

    String type = "professional"; 


    new loadQuestioners().execute(type);

    public class loadQuestioners extends AsyncTask <String, Integer, JSONArray> {

    @Override
    protected JSONArray doInBackground(String... arg0) {

        //is the arg0 is the type i passed?

        return null;
    }

}

3 Answers 3

1

Just modify the code as follows,

public class loadQuestioners extends AsyncTask <String, Integer, JSONArray> {

@Override
protected JSONArray doInBackground(String... arg0) {

    //is the arg0 is the type i passed?
    String yourType=arg0[0];


    return null;
}

"yourType" contains the type you passed while creating asyncTask

Sign up to request clarification or add additional context in comments.

3 Comments

So if I have a new loadQuestioners().execute(type, type2, type3); String yourType = arg[0]; String yourType2 = arg[1]; and so on? Nc thx
Then you can access them using the code, for(String type: arg0 ){ //do something with type passed }
w8 I try this in my project
1

I can see two ways of going about this: 1st:

new loadQuestioners().execute("professional");

public class loadQuestioners extends AsyncTask <String, Integer, JSONArray> 
{
    @Override
    protected JSONArray doInBackground(String... arg) 
    {
        String word = arg[0];
        return null;
    }
}

And 2nd: create a constructor and pass the argument:

new loadQuestioners("professional").execute();

public class loadQuestioners extends AsyncTask <String, Integer, JSONArray> 
{
    private String word;

    public loadQuestioners(String word)
    {
         this.word = word;
    }

    @Override
    protected JSONArray doInBackground(String... arg0) 
    {
        //use word
        return null;
    }
}

3 Comments

Sorry for the bad formatting, I'm posting from my phone.
No problem but making contructor inside Asynctask make me confuse because without contructor It seems it will work
yes, both ways are correct and work, it's just a matter a preference. Personally I like the 2nd method more because I find it more readable than the 1st where you have to use the array index to access the parameters.
0

String... args means that it can take as paramrters:

  • a single String object OR
  • an array of Strings

So in your case pass an array of Strings to your async task's execute method and doInBackground will get them just fine. Inside doInBackground use the arg0 parameter as a String array and you should be good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.