2013-06-19 52 views
0

我需要您的帮助才能实现以下行为:等待ListView数据

ActivityONE启动ActivityTWO,它包含ListView。当ActivityTWO启动时,ListView的数据从互联网下载。我想在ActivityTWO中显示ProgressBar,同时下载数据并随后显示带有下载数据的ListView。 ActivityTWO的哪些点应该用适当的数据“激活”ListView?而这种“推迟”应该是什么样子?

预先感谢

+3

两个词:['AsyncTask'](http://developer.android.com/reference/android/os/AsyncTask.html)和['ProgressDialog'](http://developer.android.com/reference/android/app/ProgressDialog.html)。 – m0skit0

+0

但是,如何通知适配器,有任何数据要使用? – Mariusz

+1

直到有数据时才需要创建适配器。获得数据后,填充适配器并关闭进度对话框。 – m0skit0

回答

0

尝试下面的代码

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(); 
    } 
} 
}