Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android passing String array and String to asyncTask

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.

like image 473
user2886091 Avatar asked Aug 04 '26 11:08

user2886091


2 Answers

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.

like image 131
M D Avatar answered Aug 06 '26 23:08

M D


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(...);

like image 28
johntheripp3r Avatar answered Aug 06 '26 23:08

johntheripp3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!