Android 当我调用数据库时,我需要有关线程和ProgressDialog的帮助

Android 当我调用数据库时,我需要有关线程和ProgressDialog的帮助,android,adapter,progressdialog,Android,Adapter,Progressdialog,我正在为公司事件构建应用程序,我从数据库中获取了事件,并将其填充到ListView的适配器中,我需要在从数据库检索数据的过程中显示ProgressDialog,这是我的代码 ` `移动您的 listView.setAdapter(adapter); progressDialog.dismiss(); adapter.notifyDataSetChanged(); 进入处理程序,并从线程调用处理程序的方法sendEmptyMessage()。获取适配器后运行() 考虑更多信息 编辑: 您的代码应

我正在为公司事件构建应用程序,我从数据库中获取了事件,并将其填充到
ListView
的适配器中,我需要在从数据库检索数据的过程中显示
ProgressDialog
,这是我的代码 `

`

移动您的

listView.setAdapter(adapter);
progressDialog.dismiss();
adapter.notifyDataSetChanged();
进入
处理程序
,并从
线程调用
处理程序
的方法
sendEmptyMessage()
。获取
适配器
后运行()

考虑更多信息

编辑:

您的代码应该是这样的

new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(2000);
                //this is call the webservice to got filled adapter 
                adapter = new EventWebservice().getAdapter(this);
                handler.sendEmptyMessage(0);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

处理程序将在其中更新列表。但是我要说的是利用AsyncTask()。。在preExecute()中显示ypur对话框,并在postexecute()中取消对话框;。。后台任务中的数据获取代码。。我的意思是,就像下面。。这是我在项目中使用的示例代码

类背景任务扩展异步任务 {


您需要了解未同步的ws-CAll以及如何动态填充listview中的数据。下面是代码片段,它可以工作,并将确保不会显示ws-CAll需要多长时间,GUI上不会出现中断,流程顺畅:

String wsResponse[];

public void sampleFunction()
    {


        progressDialog = ProgressDialog.show(this, "", "Getting backup list...");
        new Thread() {
            public void run() {
                try {
                    //do the ws call and store the ws response in a string array 
                    wsResponse=wsCall();

                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                messageHandler.sendEmptyMessage(0);
                //              messageHandler.sendEmptyMessage(0);
            }
        }.start();
    }
}


//inside the handler set the string array retrieved from the WS in sampleFunction to the adapter
private Handler messageHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //here write the code to assign the string array to the adapter

        }
    };
我会给我们一个答案。这是应该发生的事情的结构

    @Override
protected void onPreExecute() {
    dialog = ProgressDialog.show(context, "", "Loading. Please wait...",
            true);
}

@Override
protected EventWebservice doInBackground(Void... params) {
         //call the webservice and return it
}

@Override
protected void onPostExecute(EventWebservice webservice) {
    adapter = webservice.getAdapter(this);;
            listView.setAdapter(adapter);
    dialog.dismiss();
}

感谢您的anser adil,但我尝试这样做`新线程(new Runnable(){public void run(){try{Thread.sleep(2*1000);handler.sendEmptyMessage(0);//这是调用Web服务来填充适配器}catch(InterruptedException e){e.printStackTrace();}}).start();Handler Handler=new Handler(){@Override public void handleMessage(Message msg){//TODO自动生成的方法存根适配器=new EventWebservice().getAdapter(this);listView.setAdapter(adapter);progressDialog.dismise();}};但它不起作用
String wsResponse[];

public void sampleFunction()
    {


        progressDialog = ProgressDialog.show(this, "", "Getting backup list...");
        new Thread() {
            public void run() {
                try {
                    //do the ws call and store the ws response in a string array 
                    wsResponse=wsCall();

                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                messageHandler.sendEmptyMessage(0);
                //              messageHandler.sendEmptyMessage(0);
            }
        }.start();
    }
}


//inside the handler set the string array retrieved from the WS in sampleFunction to the adapter
private Handler messageHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //here write the code to assign the string array to the adapter

        }
    };
    @Override
protected void onPreExecute() {
    dialog = ProgressDialog.show(context, "", "Loading. Please wait...",
            true);
}

@Override
protected EventWebservice doInBackground(Void... params) {
         //call the webservice and return it
}

@Override
protected void onPostExecute(EventWebservice webservice) {
    adapter = webservice.getAdapter(this);;
            listView.setAdapter(adapter);
    dialog.dismiss();
}