即时通讯使用azure移动服务。我在db中有一些用户要进行身份验证,为了做到这一点,我在输入用户名和密码后执行查询以获取用户,然后按确定。按下确定后,如果一切正常,则应启动意图。在完成执行查询的回调方法之前,如何显示ProgressDialog?
编辑:问题是我有一个按钮(logIn按钮),当你点击它时,它将构建一个查询并在异步任务中执行它,因此我的问题。如果我只是添加一个进度对话框,调用流将继续,因为从onClickListener的角度来看,操作已经完成。
答案 0 :(得分:4)
在调用查询之前只显示()它,并在回调方法中解除()它。
答案 1 :(得分:2)
在您使用AsyncTask
查询数据时,请使用onPreExecute
和onPostExecute
方法显示/取消ProgressDialog
。
创建一个扩展AsyncTask
的类,就像这样。在onPreExecute
中显示ProgressDialog
以及完成在doInBackground
中提取数据后,在onPostExecute
中关闭对话框
public class QueryTask extends AsyncTask<Void,Void,Object> {
private ProgressDialog progressDialog = null;
private final Context mContext;
public QueryTask(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// do your stuff to query the data
return null;
}
@Override
protected void onPostExecute(Object result) {
progressDialog.dismiss();
// do your other stuff with the queried result
}
@Override
protected void onCancelled(Object result) {
progressDialog.dismiss();
}
}
最后,按钮onClick
执行任务
new QueryTask(YourActivity.this).execute();
答案 2 :(得分:1)
我使用此示例代码从SQL数据库加载所有事件。在应用程序从服务器获取数据之前,会向用户显示进度对话框。
class LoadAllEvents extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Just a moment...");
pDialog.setIndeterminate(true);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_events,
"GET", params);
try {
// Checking for SUCCESS TAG
int success = json.getInt(CONNECTION_STATUS);
if (success == 1) {
// products found
// Getting Array of Products
Events = json.getJSONArray(TABLE_EVENT);
// looping through All Contacts
for (int i = 0; i < Events.length(); i++) {
JSONObject evt = Events.getJSONObject(i);
// Storing each json item in variable
id = evt.getString(pid);
group = evt.getString(COL_GROUP);
name = evt.getString(COL_NAME);
desc = evt.getString(COL_DESC);
date = evt.getString(COL_DATE);
time = evt.getString(COL_TIME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(pid, id);
map.put(COL_GROUP, group);
map.put(COL_NAME, name);
map.put(COL_DESC, desc);
map.put(COL_DATE, date);
map.put(COL_TIME, time);
// adding HashList to ArrayList
eventsList.add(map);
}
} else {
// Options are not available or server is down.
// Dismiss the loading dialog and display an alert
// onPostExecute
pDialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(getActivity(),
eventsList, R.layout.list_item, new String[] {
pid, COL_GROUP, COL_NAME, COL_DATE, COL_TIME },
new int[] { R.id.pid, R.id.group, R.id.name, R.id.header,
R.id.title2 });
setListAdapter(adapter);
}
});
}
希望这会有所帮助。