开发者

how to update listview periodically in android

my listview is not updated periodically in android it only updates when i scroll the listview then only its adapter changes

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


Try listPlacesAdapter.notifyDataSetInvalidated();


This works perfectly for me:

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

After this you can start thread in onCreate() as:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜