我需要您的帮助来实现以下行为:
ActivityONE 启动 ActivityTWO,它持有 ListView。ListView 的数据是在 ActivityTWO 启动时从 Internet 下载的。我想在下载数据时在 ActivityTWO 中显示 ProgressBar,然后显示带有下载数据的 ListView。我应该在 ActivityTWO 的哪些点使用适当的数据“激活”ListView?这种“推迟”应该是什么样子?
提前致谢
试试下面的代码
public class YourActivity extends ListActivity{
private ProgressDialog progressBar;
private Animation rotation;
private DownloadXmlTask downloadXmlTask;
private Boolean Loading=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_main);
doDownload();
}
private void doDownload()
{
if ( isConnectedToInternet() ){
downloadXmlTask=new DownloadXmlTask();
downloadXmlTask.execute();
}
else
Toast.makeText(YourActivity.this,"faild connection", 1).show();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (Loading==true)
stopLoadingAndDownloading();
}
//=====================
//New Class Starts Here
//=====================
class IconicAdapter extends ArrayAdapter<String> {
//your class
}
public void onListItemClick(ListView parent, View v, int position,
long id){
//do something
}
/*-----------------------------------------------------------------------------------
* Showing / Stopping progress dialog which is showing loading animation
* ---------------------------------------------------------------------------------*/
private void showLoading(){
progressBar = ProgressDialog.show(YourActivity.this, "", "");
progressBar.setContentView(R.layout.YourLayout);
progressBar.setCancelable(false);
//and another thing that need for progressBar
Loading=true;
}
private void stopLoadingAndDownloading() {
Loading=false;
if(progressBar.isShowing())
progressBar.dismiss();
if (downloadXmlTask != null && downloadXmlTask.getStatus() != AsyncTask.Status.FINISHED)
downloadXmlTask.cancel(true);
}
public boolean isConnectedToInternet(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
/*********************************************************
* this class is a useful class that prevent from crashing
* when download of data take very long time
*********************************************************/
private class DownloadXmlTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
showLoading();
}
@Override
protected String doInBackground(Void... esult) {
//doing your download from internet
return msg;
}
@Override
protected void onPostExecute(String result) {
//do something
setListAdapter(new IconicAdapter());
stopLoadingAndDownloading();
}
}
}