我可以在后台迭代吗?
Can I iterate in the background?
我有一个正在冻结我的应用程序的解析查询:
ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle");
query.whereEqualTo("puzzle", "somePuzzle");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
ArrayList<Puzzle> listPuzzle = new ArrayList<>();
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));
ListView list = (ListView) findViewById(R.id.list_puzzle);
if (list != null && listPuzzle.size() != 0) {
AdapterPuzzle adapterPuzzle = new AdapterPuzzle(listPuzzle, ScreenPuzzle.this);
list.setAdapter(adapterPuzzle);
}
} else e.printStackTrace();
}
});
当我执行此查询时,activity 会冻结几秒钟,直到我填满我的 ListView。
我测试了 运行 方法 "done" 中没有内容的查询,它似乎 运行 顺利,所以我的猜测是我在 "done" 方法正在冻结 activity 因为它可能做了很多工作,特别是迭代器:
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));
有什么方法可以在后台 运行 这个迭代器或所有这些操作吗?有什么办法可以避免这种冻结?
尝试使用AsyncTaskclass。它具有完全适合您的任务的 doInBackground 方法。
编辑:
我正在为需要参考的人添加我的代码的解决方案:
public class ScreenPuzzle extends AppCompatActivity {
private ListView list;
private TextView textUnresolved;
private ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_puzzle);
list = (ListView) findViewById(R.id.list_puzzle);
textUnresolved = (TextView) findViewById(R.id.text_unresolved);
loading = (ProgressBar) findViewById(R.id.loading_rank);
ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle");
query.whereEqualTo("puzzle", "somePuzzle");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) new BackgroundOperation(objects).execute();
else e.printStackTrace();
}
});
}
private class BackgroundOperation extends AsyncTask<Void, Void, ArrayList<Puzzle>> {
private List<ParseObject> objects;
private ArrayList<Puzzle> listPuzzle;
public BackgroundOperation(List<ParseObject> objects) { this.objects = objects; }
@Override
protected ArrayList<Puzzle> doInBackground(Void... voids) {
listPuzzle = new ArrayList<>();
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));
return listPuzzle;
}
@Override
protected void onPostExecute(ArrayList<Puzzle> listPuzzle) {
if (list != null && listPuzzle.size() != 0) {
final AdapterPuzzle adapterPuzzle = new AdapterPuzzle(listPuzzle, ScreenPuzzle.this);
list.setAdapter(adapterPuzzle);
} else textUnresolved.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
}
}
我有一个正在冻结我的应用程序的解析查询:
ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle");
query.whereEqualTo("puzzle", "somePuzzle");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
ArrayList<Puzzle> listPuzzle = new ArrayList<>();
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));
ListView list = (ListView) findViewById(R.id.list_puzzle);
if (list != null && listPuzzle.size() != 0) {
AdapterPuzzle adapterPuzzle = new AdapterPuzzle(listPuzzle, ScreenPuzzle.this);
list.setAdapter(adapterPuzzle);
}
} else e.printStackTrace();
}
});
当我执行此查询时,activity 会冻结几秒钟,直到我填满我的 ListView。
我测试了 运行 方法 "done" 中没有内容的查询,它似乎 运行 顺利,所以我的猜测是我在 "done" 方法正在冻结 activity 因为它可能做了很多工作,特别是迭代器:
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));
有什么方法可以在后台 运行 这个迭代器或所有这些操作吗?有什么办法可以避免这种冻结?
尝试使用AsyncTaskclass。它具有完全适合您的任务的 doInBackground 方法。
编辑:
我正在为需要参考的人添加我的代码的解决方案:
public class ScreenPuzzle extends AppCompatActivity {
private ListView list;
private TextView textUnresolved;
private ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_puzzle);
list = (ListView) findViewById(R.id.list_puzzle);
textUnresolved = (TextView) findViewById(R.id.text_unresolved);
loading = (ProgressBar) findViewById(R.id.loading_rank);
ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle");
query.whereEqualTo("puzzle", "somePuzzle");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) new BackgroundOperation(objects).execute();
else e.printStackTrace();
}
});
}
private class BackgroundOperation extends AsyncTask<Void, Void, ArrayList<Puzzle>> {
private List<ParseObject> objects;
private ArrayList<Puzzle> listPuzzle;
public BackgroundOperation(List<ParseObject> objects) { this.objects = objects; }
@Override
protected ArrayList<Puzzle> doInBackground(Void... voids) {
listPuzzle = new ArrayList<>();
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));
return listPuzzle;
}
@Override
protected void onPostExecute(ArrayList<Puzzle> listPuzzle) {
if (list != null && listPuzzle.size() != 0) {
final AdapterPuzzle adapterPuzzle = new AdapterPuzzle(listPuzzle, ScreenPuzzle.this);
list.setAdapter(adapterPuzzle);
} else textUnresolved.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
}
}