을 실행 onClick
때 onPreExecute
에서 ProgressDialog
을 표시하고 onPostExecute
에, doInBackground
의 데이터를 가져 오는 함께 할 때 마지막으로 대화
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();
}
}
을 기각,이 예제 코드에 의해 사용되었다 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);
}
});
}
희망이 도움이됩니다.
일부 코드를 게시하십시오. 여기서 콜백이 어디에 있는지 알려면 asynctask를 사용하는 경우 데이터 준비가 완료된 콜백을 얻을 수 있습니다. – tyczj
해당 작업을 수행하는 안드로이드 AsyncTask가 아니어야합니다. 나는 콜백에서 그것을 무시하고 작동 할 것이다. 그러나 나머지 코드는 여전히 기본적으로 실행되어 결국 전혀 도움이되지 않는다. – diazazar
문제가 발생하는 곳에서 코드를 표시해야합니다. – tyczj