如何使用AsyncTask for Android显示进度对话框?
问题是进度对话框不显示。这可能是什么原因造成的 我的活动的onCreate:如何使用AsyncTask for Android显示进度对话框?,android,parse-platform,android-asynctask,progressdialog,Android,Parse Platform,Android Asynctask,Progressdialog,问题是进度对话框不显示。这可能是什么原因造成的 我的活动的onCreate: public class NewsActivity extends AppCompatActivity { //Defining Variables protected Toolbar toolbar; protected NavigationView navigationView; private DrawerLayout drawerLayout; protected Button mButtonHeader
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();
}
}
//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;
}
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
}
公共类NewsActivity扩展了AppCompatActivity{
//定义变量
受保护的工具栏;
受保护的导航视图导航视图;
私人抽屉布局;
受保护的按钮MButonheader;
私人回收视图mRecyclerView;
私人新闻服务;
最终列表数据=新的ArrayList();
私人SwipeRefreshLayout mSwipeRefreshLayout;
私人语境;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
ProgressDialog进度=新建ProgressDialog(此);
progress.setMessage(“加载…”);
新建MyTask(progress.execute();
}
}
我的活动中的异步任务:
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();
}
}
//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;
}
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
}
//处理加载列表的繁重后台过程的类
类MyTask扩展了AsyncTask{
公共MyTask(进度对话框进度){
这个。进步=进步;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progress.setMessage(“加载…”);
progress.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//这将在后台执行。
parseQueryNewsTable();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
进步。解散();
}
进程对话进程;
}
My activity中包含要加载到AsyncTask中的数据的My方法:
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();
}
}
//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;
}
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
}
void parseQueryNewsTable(){
ParseQuery=ParseQuery.getQuery(“新闻”);
query.orderByDescending(“createdAt”);
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
对于(int i=0;i
在调用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
}
void parseQueryNewsTable(){
//在此显示进展
ParseQuery=ParseQuery.getQuery(“新闻”);
query.orderByDescending(“createdAt”);
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
对于(int i=0;i