2

I would like to pass String array and String variable to my AsyncTask class. I am new to programming android apps so It is possible that this is not the most efficient way to achieve my goal. Anyways, I have String array called separate[] and String selected.

In separate[] I have loaded values from EditText View and in selected, there is a value from my spinner. Now I want to work with these in my AsyncTask. My AsyncTask class looks now like this:

 final class cyklus extends AsyncTask<String[], Void, String[]>{
String[] tones = {"C","Cis","D","Dis","E","F","Fis","G","Gis","A","Ais","B"};
String[] result;

@Override
protected String[] doInBackground(String[]... params) {

    int l =params.length;  //length of separate[]

    for(int k=0; k==l; k++){   // finding indexes of matches of elements separate[k] in tones[] 

        // INPUT POSITION
                int i= Arrays.asList(tones).indexOf(params[k]);


        // RESULT INDEX
                int j =Integer.parseInt(selected);
                int index = i+j;    


        // RESULT
                String res=tones[index];
                result[k]=res;                          

}

    return result;
}
 }

After this for loop is done, I would like my AsyncTask to return result[]. To sum up tis, I would like to know how can I work with "separate[]" and "selected" in my AsyncTask class. Thank you.

EDIT: One more subquestion. My for loop wont start. Why? Thank you.

4 Answers 4

8

Change your method to look like this:defined globally

String[] tones = {"C","Cis","D","Dis","E","F","Fis","G","Gis","A","Ais","B"};

now, call your Async task like below:

new cyklus().execute(tones);

And now, change your Async task implementation

public class cyklus extends AsyncTask<String[], Void, String[]> {
ProgressDialog dialog;

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(context);
    dialog.setTitle("Calculating...");
    dialog.setMessage("Please wait...");
    dialog.setIndeterminate(true);
    dialog.show();
}

protected String[] doInBackground(String[]... passing) {
    String[] result = new String[10];
    String[] passed = passing[0]; //get passed array

    //Some calculations...

    return result; //return result
}

protected void onPostExecute(String[] result) {
    dialog.dismiss();
    String minim = result.get(0);
}

This is how you implement AsyncTask. Hope this helps you.

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

4 Comments

Thank you, but I think we didnt understand each other :). I dont need to pass my tones[] in. I need to pass one string variable: String selected and String[] separate, where in String selected, there is value from my spinner and in String[] separate, there are values from my EditText. String selected is declared in my onCreate method. How can I do this?
@user2886091 so you want pass a string value alright and what you want get return back from AsyncTask?
I want to pass String[] separate and String selected at the same time. I want my AsyncTask to return String[] result.
Create a AsyncTask Constructor with Two Argument. Have a look into @ohntheripp3r Answer below
1

Create a asynctask constructor like this

public class Blah extends AsyncTask<... , ... , ...>
{
     String one;
     String[] two;
     public Blah(String one, String[] two)
     {
         this.one = one;
         this.two = two;
     }
     onPreExe.....
     doInBack....
     onPostExe....
 }

From Activity

Blah b = new Blah("abc", new String[]{"a", "b","c"}; b.execute(...);

Comments

1

Write a constructor and pass values using it.

    public cyklus(Context context,String[] tones){
    this.context = context;
    this.tones = tones;
}

Comments

0

I don't know from where selected value is coming in

// RESULT INDEX
int j =Integer.parseInt(selected);

But you can set the result like this by adding onPostExecute()

    final class cyklus extends AsyncTask<String[], Void, String[]> {
    String[] tones = { "C", "Cis", "D", "Dis", "E", "F", "Fis", "G", "Gis",
            "A", "Ais", "B" };
    String[] result;

    @Override
    protected String[] doInBackground(String[]... params) {

        int l = params.length; // length of separate[]

        for (int k = 0; k == l; k++) { // finding indexes of matches of
                                        // elements separate[k] in tones[]

            // INPUT POSITION
            int i = Arrays.asList(tones).indexOf(params[k]);

            // RESULT INDEX
            int j = Integer.parseInt(selected);
            int index = i + j;

            // RESULT
            String res = tones[index];
            result[k] = res;

        }

        return result;
    }

    @Override
    protected void onPostExecute(String[] result) 
    {
        super.onPostExecute(result);
        //Set the result array to what ever object u want to
    }
}

1 Comment

That is the thing I am asking. String selected is declared in my onCreate method and in this varieble, there is value from my spinner. So I need to pass in String selected and String[] separate. Can u help me with this please?

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.