Java android将字符串数组和字符串传递给asyncTask

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20868167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:52:38  来源:igfitidea点击:

android passing String array and String to asyncTask

javaandroidparametersandroid-asynctask

提问by user2886091

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.

我想将字符串数组和字符串变量传递给我的 AsyncTask 类。我是编写 android 应用程序的新手,所以这可能不是实现我的目标的最有效方法。无论如何,我有一个名为separate[] 的String 数组,并选择了String。

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:

在单独的 [] 中,我从 EditText 视图中加载了值,并且在选择中,我的微调器中有一个值。现在我想在我的 AsyncTask 中使用这些。我的 AsyncTask 类现在看起来像这样:

 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.

在这个 for 循环完成后,我希望我的 AsyncTask 返回结果 []。总而言之,我想知道如何在我的 AsyncTask 类中使用“separate[]”和“selected”。谢谢你。

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

编辑:还有一个子问题。我的 for 循环无法启动。为什么?谢谢你。

回答by johntheripp3r

Create a asynctask constructor like this

创建一个像这样的 asynctask 构造函数

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

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

回答by deniz

Write a constructor and pass values using it.

编写一个构造函数并使用它传递值。

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

回答by Tabrej Khan

I don't know from where selectedvalue is coming in

我不知道选定的值来自哪里

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

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

但是你可以通过添加 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
    }
}

回答by M D

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.

这就是您实现 AsyncTask 的方式。希望这对你有帮助。