等待ListView数据

时间:2013-06-19 08:19:05

标签: android listview

我需要你的帮助来实现以下行为:

ActivityONE启动ActivityTWO,它包含ListView。当ActivityTWO启动时,ListView的数据将从Internet下载。我想在下载数据时在ActivityTWO中显示ProgressBar,然后显示带有下载数据的ListView。我应该使用适当的数据“激活”ListView中的ActivityTWO点?这种'推迟'应该如何?

提前致谢

1 个答案:

答案 0 :(得分: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();
    }
}
}