gpt4 book ai didi

java - android 将 String 数组和 String 传递给 asyncTask

转载 作者:太空狗 更新时间:2023-10-29 15:53:36 24 4
gpt4 key购买 nike

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

在单独的 [] 中,我从 EditText View 加载了值,在选择中,我的微调器有一个值。现在我想在我的 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;
}
}

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

编辑:另一个子问题。我的 for 循环不会开始。为什么?谢谢。

最佳答案

将您的方法更改为如下所示:全局定义

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

现在,像下面这样调用您的异步任务:

new cyklus().execute(tones);

现在,更改您的异步任务实现

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);
}

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

关于java - android 将 String 数组和 String 传递给 asyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20868167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com