Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在等待ListView数据_Android_Listview - Fatal编程技术网

Android 正在等待ListView数据

Android 正在等待ListView数据,android,listview,Android,Listview,我需要你的帮助来实现以下行为: ActivityONE启动Activity2,后者保存ListView。当ActivityTWO启动时,ListView的数据从internet下载。我想在数据下载时在Activity2中显示一个进度条,然后用下载的数据显示ListView。在哪些活动点中我应该使用适当的数据“激活”列表视图?这种“推迟”应该是什么样子 提前感谢请尝试下面的代码 public class YourActivity extends ListActivity{ private

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

ActivityONE启动Activity2,后者保存ListView。当ActivityTWO启动时,ListView的数据从internet下载。我想在数据下载时在Activity2中显示一个进度条,然后用下载的数据显示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();
    }
}
}
公共类YourActivity扩展了ListActivity{
私人ProgressDialog progressBar;
私有动画旋转;
私有下载XmlTask下载XmlTask;
私有布尔加载=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_main);
doDownload();
}
私有void doDownload()
{
如果(断开连接的点网络()){
downloadXmlTask=新的downloadXmlTask();
下载xmltask.execute();
}
其他的
Toast.makeText(YourActivity.this,“连接失败”,1.show();
}
@凌驾
受保护的void onPause(){
//TODO自动生成的方法存根
super.onPause();
如果(加载==true)
停止加载和下载();
}
//=====================
//新课从这里开始
//=====================
类IconicAdapter扩展了ArrayAdapter{
//你们班
}
public void onListItemClick(ListView父视图、视图v、整型位置、,
长id){
//做点什么
}
/*-----------------------------------------------------------------------------------
*显示/停止正在加载动画的进度对话框
*  ---------------------------------------------------------------------------------*/
私有void showLoading(){
progressBar=ProgressDialog.show(YourActivity.this,“,”);
progressBar.setContentView(R.layout.YourLayout);
progressBar.setCancelable(假);
//还有一件事是progressBar需要做的
加载=真;
}
私有void停止加载和下载(){
加载=假;
if(progressBar.isShowing())
progressBar.disclose();
if(downloadXmlTask!=null&&downloadXmlTask.getStatus()!=AsyncTask.Status.FINISHED)
downloadXmlTask.cancel(true);
}
公共布尔值isconnectedPointerNet(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo netInfo=cm.getActiveNetworkInfo();
如果(netInfo!=null&&netInfo.isConnectedOrConnecting()){
返回true;
}
返回false;
}
/*********************************************************
*此类是防止崩溃的有用类
*当下载数据需要很长时间时
*********************************************************/
私有类DownloadXmlTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
showLoading();
}
@凌驾
受保护的字符串背景(无效…esult){
//从网上下载吗
返回味精;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//做点什么
setListAdapter(新图标适配器());
停止加载和下载();
}
}
}

两个字:和。但是我如何通知适配器有数据要使用?在有数据之前无需创建适配器。获得数据后,填充适配器并关闭进度对话框。adapter.notifyDataSetChanged()(这是通知适配器的方式)。感谢大家快速给出答案。我的问题解决了。