在我的应用程序中,我正在从 URL 检索一些数据,因此,我在应用程序中实现了进度条,AsyncTask
但应用程序既不显示进度条,也不显示我的数据。如果我loadDataFromURL()
在方法中调用方法,onCreate()
那么我的数据会加载,但是我必须等待一段时间。请在下面检查我的代码。
public class MainActivity extends Activity
{
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
}
private void loadDataFromURL()
{
// Here I load some data from URL
}
ProgressDialog dialog;
class LoadData extends AsyncTask<Void, Void, Void>
{
protected void onPreExecute()
{
dialog = ProgressDialog.show(context, "Loading...", null);
}
protected Void doInBackground(Void... unused)
{
try
{
loadDataFromURL();
Log.d("-DONE", "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
}
catch(Exception e)
{
Log.d("-ERORR", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
}
return null;
}
protected void onProgressUpdate(Void... unused)
{
}
protected void onPostExecute(Void unused)
{
dialog.dismiss();
}
}
}