gpt4 book ai didi

java - Android - 在单独的线程中从 for 循环更新进度条

转载 作者:行者123 更新时间:2023-11-29 23:26:15 24 4
gpt4 key购买 nike

我是 android 新手,对线程处理经验不多。我想创建一个实际进展的进度条。进度条是必需的,因为 for 循环处理一个包含大约 370,000 个单词的数组,并将与用户输入匹配的单词附加到 TextView。这可能需要 1-10 秒,具体取决于用户输入的精确程度。鉴于进度条必须在单独的线程上运行,有没有一种方法可以从 forloop 内部更新它的进度?

这是前面提到的forloop的方法(第15行设置了一个可以用来更新进度条的值,第16行假设更新了progressBar):

    public void matchWords(char[] exLettersArray, String word, String[] array, ProgressBar progressBar) {
char[] guessWordChars;
char[] curWordChars;
int wordLength = word.length();
guessWordChars = word.toCharArray();
boolean matches = true;
char unknown = '-';
TextView generatedList = findViewById(R.id.generated_list);
generatedList.setMovementMethod(new ScrollingMovementMethod());

int progress;

for (int i = 0; i < array.length; i++) { //Iterates through every word in the array

progress = ((i + 1) / array.length) * 100; //Convert to %
//progressBar.setProgress(progress);

if(array[i].length() == wordLength) {
curWordChars = array[i].toCharArray();
for(int j = 0; j < wordLength && matches == true; j++) { //Iterates through every character in a word
for(int k = 0; k < exLettersArray.length; k++) { //Iterates through all letters to be excluded
if(curWordChars[j] == exLettersArray[k]) { //Makes sure that the current word does not contain exLetter
matches = false;
}
}
if(guessWordChars[j] == unknown) {
//System.out.println("found unknown variable at index " + j);
for(int q = 0; q < wordLength; q++) {
if(curWordChars[j] == guessWordChars[q]) { //Makes sure that already known letters are not used for unknown letters
matches = false;
}
}
}
else {
if(guessWordChars[j] == curWordChars[j]) {
//System.out.println(array[i] + " passed comparison of element " + j + " flag is " + matches);
}
else {
matches = false;
//System.out.println("guessWord char " + guessWordChars[j] + " != " + "curWordChars " + curWordChars[j] + " set flag to " + matches );
}
}
}
if(matches == true) {
generatedList.append(array[i]);
generatedList.append("\n");
}
}
matches = true;
}
}

最佳答案

AsyncTask 就是您要找的,https://developer.android.com/reference/android/os/AsyncTask .

您可能希望将 for 循环放在 doInBackground 方法中,并且可以在 onProgressUpdate 中更新您的 UI。执行时间超过几毫秒或阻塞 UI 的任何事情,您都应该在主线程之外执行。这就是您将代码添加到 doInBackground 方法的原因。

onPostExecutedoInBackground 中的代码完成并在主线程上执行后运行。您可以在此处更新您的用户界面。

这是一个基于您的代码的非常简短的示例:

private static class MatchWordsTask extends AsyncTask<String[], Integer, Boolean> {
private WeakReference<ProgressBar> mProgressBar;
private String[] mArray;

MatchWordsTask(ProgressBar progressBar, String[] array) {
mProgressBar = new WeakReference<ProgressBar>(progressBar);
mArray = array;
}

protected Boolean doInBackground(String[]... arrays) {
for (int i = 0; i < mArray.length; i++) {
publishProgress(((i + 1) / mArray.length) * 100);

try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
}

return true;
}

protected void onProgressUpdate(Integer... progress) {
mProgressBar.get().setProgress(progress[0]);
}

protected void onPostExecute(Boolean matches) {
Log.d(TAG, "Matches: " + matches);
}
}

您将像这样执行此 AsyncTask:

String[] array = new String[]{"This", "is", "an", "array", "of", "words"};
new MatchWordsTask(progressBar, array).execute(array);

关于java - Android - 在单独的线程中从 for 循环更新进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53492780/

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