如何在android中定期更新listview

时间:2011-09-14 13:33:32

标签: android

我的listview不会在android中定期更新 它只会在我滚动列表视图时更新,然后才会更改其适配器

final Thread dataThread=new Thread(new Runnable() {

        public void run()
        {
            getFiveNearShops();

            DataBaseHelper myDbHelper = new DataBaseHelper(mContext);
            myDbHelper = new DataBaseHelper(mContext);

            try
            {
                myDbHelper.createDataBase();
            } 
            catch (IOException ioe) 
            {
                throw new Error("Unable to create database");
            }

            try 
            {
                myDbHelper.openDataBase();
                mCountyAndShops = myDbHelper.getCountyList();
                shopCountyData = myDbHelper.getShopCountyData();
            }
            catch(SQLException sqle)
            {

                throw sqle;

            }
            finally
            {
                myDbHelper.close();
            }
        }
    });
    dataThread.start();

    Thread displayThread = new Thread(new Runnable() {
        public void run() {

         try
         {
             dataThread.join();
         }
         catch (InterruptedException e) 
         {
            e.printStackTrace();
         }  
         mHandler.post(new Runnable() { 
            public void run() 
            {
                EfficientAdapter listPlacesAdapter = new EfficientAdapter(getApplicationContext());
                list_places.setAdapter(listPlacesAdapter);
                list_places.setDivider(null);
                mapOverlays.add(itemizedoverlay);

                listPlacesAdapter.notifyDataSetChanged();

                list_county.setAdapter(new CountyListEfficientAdapter(getApplicationContext(),mCountyAndShops));
                list_county.setDivider(null);
            }
        });

        }
    });
    displayThread.start();

2 个答案:

答案 0 :(得分:0)

尝试listPlacesAdapter.notifyDataSetInvalidated();

答案 1 :(得分:0)

这对我很有用:

private class RefreshThread extends Thread{
            public boolean isStop = false;

            public void run(){
                try{
                    while(!isStop){                                             
                        YourrListActivity.this.runOnUiThread(new Runnable() {                   
                            @Override
                            public void run() {
                                   //List refresh logic goes here   
                                    //yourListView.invalidateViews();   
                                   //yourListView.notifyDataSetInvalidated();
                            }
                        });                         
                        try{ Thread.sleep(5000); } catch(Exception ex){}
                    }
            }catch(Exception e){
            }
          }//run
        }//thread

在此之后,您可以在onCreate()中启动线程:

    RefreshThread refreshThread = new RefreshThread();
    refreshThread.isStop = false;
    refreshThread.start();