如何使用 Android 的 AsyncTask 显示进度对话框?
How do I display the progress dialog using AsyncTask for Android?
问题是进度对话框没有显示。可能是什么原因造成的?
我的 Activity 的 onCreate:
public class NewsActivity extends AppCompatActivity {
//Defining Variables
protected Toolbar toolbar;
protected NavigationView navigationView;
private DrawerLayout drawerLayout;
protected Button mButtonHeader;
private RecyclerView mRecyclerView;
private NewsRVCustomAdapter mAdapter;
final List<Information> data = new ArrayList<>();
private SwipeRefreshLayout mSwipeRefreshLayout;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();
}
}
我的 activity:
我的 AsyncTask
//Class that handles the heavy background process of loading the list
class MyTask extends AsyncTask<Void, Void, Void> {
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setMessage("Loading...");
progress.show();
}
@Override
protected Void doInBackground(Void... arg0) {
//This will be executed in background.
parseQueryNewsTable();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
}
ProgressDialog progress;
}
我的 activity 中的方法包含要在 AsyncTask 中加载的数据:
void parseQueryNewsTable() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.mNewsTitle = objects.get(i).getString("name");
information.mNewsStory = objects.get(i).getString("shortinfo");
//news table from parse
information.mNewsType = objects.get(i).getString("type");
information.mNewsInformation = objects.get(i).getString("info");
String[] imgUrl = new String[objects.size()];
//Each image is obtained from the object and their URLS and stored in a string Array
//This array will be passe to the adapter in a TextView containing all the
//URLS to be stored in another Array that holds the URLS as string and bind to the ViewHolder
imgUrl[i] = objects.get(i).getParseFile("image").getUrl();
//setting the urls to the adapter
information.setNewsUrls(imgUrl[i]);
//setting image file to the drawable
information.setNewsPhotoID(R.drawable.ndtc_navlogo);
data.add(information);
}//End if
} else {
//sorry there was a problem, advice user
Toast.makeText(getApplicationContext(), "A problem occurred. May be due to an internet or data connection", Toast.LENGTH_LONG).show();
}//End else
//initialize the recycler view
RecyclerView rv = (RecyclerView)findViewById(R.id.news_recycler_view_layout);
//initialize adapter
NewsRVCustomAdapter mAdapter = new NewsRVCustomAdapter(getApplicationContext(), data);
//use recycler view initialized variable to set adapter to current state
rv.setAdapter(mAdapter);
//notify data changes
mAdapter.notifyDataSetChanged();
}
});//End Query
}
您应该在调用 findInBackground
函数之前显示进度对话框,并在完成所有操作后隐藏进度条。
void parseQueryNewsTable() {
// Show progress here
ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.mNewsTitle = objects.get(i).getString("name");
information.mNewsStory = objects.get(i).getString("shortinfo");
//news table from parse
information.mNewsType = objects.get(i).getString("type");
information.mNewsInformation = objects.get(i).getString("info");
String[] imgUrl = new String[objects.size()];
//Each image is obtained from the object and their URLS and stored in a string Array
//This array will be passe to the adapter in a TextView containing all the
//URLS to be stored in another Array that holds the URLS as string and bind to the ViewHolder
imgUrl[i] = objects.get(i).getParseFile("image").getUrl();
//setting the urls to the adapter
information.setNewsUrls(imgUrl[i]);
//setting image file to the drawable
information.setNewsPhotoID(R.drawable.ndtc_navlogo);
data.add(information);
}//End if
} else {
//sorry there was a problem, advice user
Toast.makeText(getApplicationContext(), "A problem occurred. May be due to an internet or data connection", Toast.LENGTH_LONG).show();
}//End else
//initialize the recycler view
RecyclerView rv = (RecyclerView)findViewById(R.id.news_recycler_view_layout);
//initialize adapter
NewsRVCustomAdapter mAdapter = new NewsRVCustomAdapter(getApplicationContext(), data);
//use recycler view initialized variable to set adapter to current state
rv.setAdapter(mAdapter);
//notify data changes
mAdapter.notifyDataSetChanged();
// Hide progress here
}
});//End Query
}
问题是进度对话框没有显示。可能是什么原因造成的?
我的 Activity 的 onCreate:
public class NewsActivity extends AppCompatActivity {
//Defining Variables
protected Toolbar toolbar;
protected NavigationView navigationView;
private DrawerLayout drawerLayout;
protected Button mButtonHeader;
private RecyclerView mRecyclerView;
private NewsRVCustomAdapter mAdapter;
final List<Information> data = new ArrayList<>();
private SwipeRefreshLayout mSwipeRefreshLayout;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();
}
}
我的 activity:
我的 AsyncTask //Class that handles the heavy background process of loading the list
class MyTask extends AsyncTask<Void, Void, Void> {
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setMessage("Loading...");
progress.show();
}
@Override
protected Void doInBackground(Void... arg0) {
//This will be executed in background.
parseQueryNewsTable();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
}
ProgressDialog progress;
}
我的 activity 中的方法包含要在 AsyncTask 中加载的数据:
void parseQueryNewsTable() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.mNewsTitle = objects.get(i).getString("name");
information.mNewsStory = objects.get(i).getString("shortinfo");
//news table from parse
information.mNewsType = objects.get(i).getString("type");
information.mNewsInformation = objects.get(i).getString("info");
String[] imgUrl = new String[objects.size()];
//Each image is obtained from the object and their URLS and stored in a string Array
//This array will be passe to the adapter in a TextView containing all the
//URLS to be stored in another Array that holds the URLS as string and bind to the ViewHolder
imgUrl[i] = objects.get(i).getParseFile("image").getUrl();
//setting the urls to the adapter
information.setNewsUrls(imgUrl[i]);
//setting image file to the drawable
information.setNewsPhotoID(R.drawable.ndtc_navlogo);
data.add(information);
}//End if
} else {
//sorry there was a problem, advice user
Toast.makeText(getApplicationContext(), "A problem occurred. May be due to an internet or data connection", Toast.LENGTH_LONG).show();
}//End else
//initialize the recycler view
RecyclerView rv = (RecyclerView)findViewById(R.id.news_recycler_view_layout);
//initialize adapter
NewsRVCustomAdapter mAdapter = new NewsRVCustomAdapter(getApplicationContext(), data);
//use recycler view initialized variable to set adapter to current state
rv.setAdapter(mAdapter);
//notify data changes
mAdapter.notifyDataSetChanged();
}
});//End Query
}
您应该在调用 findInBackground
函数之前显示进度对话框,并在完成所有操作后隐藏进度条。
void parseQueryNewsTable() {
// Show progress here
ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.mNewsTitle = objects.get(i).getString("name");
information.mNewsStory = objects.get(i).getString("shortinfo");
//news table from parse
information.mNewsType = objects.get(i).getString("type");
information.mNewsInformation = objects.get(i).getString("info");
String[] imgUrl = new String[objects.size()];
//Each image is obtained from the object and their URLS and stored in a string Array
//This array will be passe to the adapter in a TextView containing all the
//URLS to be stored in another Array that holds the URLS as string and bind to the ViewHolder
imgUrl[i] = objects.get(i).getParseFile("image").getUrl();
//setting the urls to the adapter
information.setNewsUrls(imgUrl[i]);
//setting image file to the drawable
information.setNewsPhotoID(R.drawable.ndtc_navlogo);
data.add(information);
}//End if
} else {
//sorry there was a problem, advice user
Toast.makeText(getApplicationContext(), "A problem occurred. May be due to an internet or data connection", Toast.LENGTH_LONG).show();
}//End else
//initialize the recycler view
RecyclerView rv = (RecyclerView)findViewById(R.id.news_recycler_view_layout);
//initialize adapter
NewsRVCustomAdapter mAdapter = new NewsRVCustomAdapter(getApplicationContext(), data);
//use recycler view initialized variable to set adapter to current state
rv.setAdapter(mAdapter);
//notify data changes
mAdapter.notifyDataSetChanged();
// Hide progress here
}
});//End Query
}