AsyncTask onProgressUpdate 被重复调用
AsyncTask onProgressUpdate is called repeatedly
问题
AsyncTask onProgressUpdate连续调用多次,不会停止。 onProgressUpdate 每秒连续调用 +1000 次。
- publishProgress() 被调用了 3 次。 (如预期)
- onPostExecute 被执行,但是onProgressUpdate 继续被调用。
- 我在应用程序中有另一个以类似方式应用的 AsynTask,它工作得很好。
特征
我有一个项目网格,经过这些项目直到它停在选定的项目处。通过更改 RelativeLayout 的背景来显示选择。
简化代码
public class test extends Activity {
GridView gridView;
...
private class Async_wordletNewCharacter extends AsyncTask<String, Integer, Boolean> {
RelativeLayout gridItem;
RelativeLayout itemSel;
Drawable goldDrawable = getResources().getDrawable(R.drawable.sh_circle_gold);
public Async_wordletNewCharacter(String param) {super();}
@Override
protected Boolean doInBackground(String... params) {
try {
ArrayList<Integer> positionList = new ArrayList<>(Arrays.asList(1, 2, 3));
for (int i = 0; i < positionList.size(); i++) {
int position = positionList.get(i);
gridItem = (RelativeLayout) gridView.getChildAt(position);
itemSel = (RelativeLayout) gridItem.findViewById(R.id.item_selector);
publishProgress(0); // Circle the selected item
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.publishProgress(values);
itemSel.setBackground(goldDrawable);
}
protected void onPostExecute(Boolean a) {
Log.d(LOG, "async_wait : onPostExecute");
}
}
}
如有任何帮助,我们将不胜感激。提前致谢!
super.publishProgress(values);
是罪魁祸首。显然,您的意思是 super.onProgressUpdate(values);
对吗?
问题
AsyncTask onProgressUpdate连续调用多次,不会停止。 onProgressUpdate 每秒连续调用 +1000 次。
- publishProgress() 被调用了 3 次。 (如预期)
- onPostExecute 被执行,但是onProgressUpdate 继续被调用。
- 我在应用程序中有另一个以类似方式应用的 AsynTask,它工作得很好。
特征
我有一个项目网格,经过这些项目直到它停在选定的项目处。通过更改 RelativeLayout 的背景来显示选择。
简化代码
public class test extends Activity {
GridView gridView;
...
private class Async_wordletNewCharacter extends AsyncTask<String, Integer, Boolean> {
RelativeLayout gridItem;
RelativeLayout itemSel;
Drawable goldDrawable = getResources().getDrawable(R.drawable.sh_circle_gold);
public Async_wordletNewCharacter(String param) {super();}
@Override
protected Boolean doInBackground(String... params) {
try {
ArrayList<Integer> positionList = new ArrayList<>(Arrays.asList(1, 2, 3));
for (int i = 0; i < positionList.size(); i++) {
int position = positionList.get(i);
gridItem = (RelativeLayout) gridView.getChildAt(position);
itemSel = (RelativeLayout) gridItem.findViewById(R.id.item_selector);
publishProgress(0); // Circle the selected item
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.publishProgress(values);
itemSel.setBackground(goldDrawable);
}
protected void onPostExecute(Boolean a) {
Log.d(LOG, "async_wait : onPostExecute");
}
}
}
如有任何帮助,我们将不胜感激。提前致谢!
super.publishProgress(values);
是罪魁祸首。显然,您的意思是 super.onProgressUpdate(values);
对吗?