Java 我可以在后台迭代吗?
我有一个正在冻结我的应用程序的解析查询:Java 我可以在后台迭代吗?,java,android,parse-platform,background,freeze,Java,Android,Parse Platform,Background,Freeze,我有一个正在冻结我的应用程序的解析查询: ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle"); query.whereEqualTo("puzzle", "somePuzzle"); query.findInBackground(new FindCallback<ParseObject>() { public void done(List<ParseObject> objec
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();
}
});
有没有办法在后台运行此迭代器或所有这些操作?有没有办法避免这种冻结?尝试使用类。它有一种完全适合您的任务的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);
}
}
}
公共类ScreenPuzzle扩展了AppCompative活动{
私有列表视图列表;
私有文本视图文本未解析;
私家酒吧;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_拼图);
list=(ListView)findViewById(R.id.list\u);
textUnresolved=(TextView)findViewById(R.id.text\u unresolved);
加载=(ProgressBar)findViewById(R.id.loading\u rank);
ParseQuery查询=新的ParseQuery(“拼图”);
查询:whereEqualTo(“拼图”、“某物拼图”);
findInBackground(新的FindCallback(){
公共void done(列出对象,parsee异常){
如果(e==null)新建后台操作(objects).execute();
否则,例如printStackTrace();
}
});
}
私有类BackgroundOperation扩展了异步任务{
私有列表对象;
私有数组列表;
公共后台操作(列出对象){this.objects=objects;}
@凌驾
受保护的ArrayList doInBackground(无效…无效){
listPuzzle=新的ArrayList();
对于(ParseObject对象:对象)listPuzzle.add(新建Puzzle(对象));
返回列表;
}
@凌驾
受保护的void onPostExecute(ArrayList listPuzzle){
if(list!=null&&listPuzzle.size()!=0){
最终AdapterPuzzle AdapterPuzzle=新AdapterPuzzle(列表拼图,屏幕拼图,this);
列表.setAdapter(adapterPuzzle);
}else textUnresolved.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
}
}
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);
}
}
}