我已将解析方法放置在 onCreate
方法中。但我的问题是如何显示 Android Loading... 对话框?
Parse.initialize(this, "a", "b");
ParseQuery query = new ParseQuery("Category");
query.findInBackground(new FindCallback() {
@Override
public void done(List<ParseObject> catObjects, ParseException arg1) {
Log.d("Catlength", String.valueOf(catObjects.size()));
for(int i =0; i<catObjects.size(); i++){
Log.d("lengthName"+String.valueOf(i), String.valueOf(catObjects.get(i).getInt("Id")));
Category category = new Category();
category.Name= catObjects.get(i).getString("CatName");
category.id= catObjects.get(i).getInt("Id");
categories.add(category);
}
if(categories.size()>0){
setListAdapter(new CategoryArrayAdapter(CategoryListActivity.this, R.layout.row_category, categories));
}
else{
Toast.makeText(CategoryListActivity.this, "Our servers are busy. Hit refresh..", 3000).show();
}
}
});
上面的代码一切正常,但我不知道如何显示对话框。
我无法使用 AsycTask
,因为解析 sdk 在后台调用自己的线程,并且在 findInBackground
执行完成之前,doInBackground
> 完成 Asyc 线程。这就是我在主线程中调用它的原因。
结果,我的 ArrayList 中总是没有结果。
有人可以启发我吗?
最佳答案
关于进度对话框,我也遇到了同样的情况,尝试了一些技巧,最后只是声明了一个 ProgressDialog 类成员:
protected ProgressDialog proDialog;
然后创建了两个方法:
protected void startLoading() {
proDialog = new ProgressDialog(this);
proDialog.setMessage("loading...");
proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
proDialog.setCancelable(false);
proDialog.show();
}
protected void stopLoading() {
proDialog.dismiss();
proDialog = null;
}
并在后台操作之前调用startLoading()和stopLoading() 得到结果后在后台运行。
startLoading();
ParseUser.logInInBackground(userName.getText().toString(), hashedPass, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.d(Constants.TAG, "User Loged in.");
ParseManager.sCurrentUser = user;
stopLoading();
finish();
} else {
stopLoading();
invalidCreds();
}
}
});
如果你想使用AsyncTask,不要调用findInBackground(),你可以使用find()。 你可以在api中查看 https://parse.com/docs/android/api/com/parse/ParseQuery.html#find()
希望这有帮助。
关于java - Parse.com Android API 和 Android 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15299921/