2017-12-02 84 views
-1
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       data =""; 

        String splithis; 
       splithis=mySpinner.getSelectedItem().toString(); 
       splited = splithis.split(" "); 
       course = splited[0]; 
       sections = splited[2]; 
       new Thread(new Runnable() { 
        public void run() { 

         data = bw.getAttendanceFromDB(term, course,sections,day); 
         runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 

           ArrayList<Properties> attendanceusers = attendanceUse(data); 
           addAttendance(attendanceusers);      
          } 
         }); 
        } 
       }).start(); 
      } 

我有ArrayList attendanceusers填充我的動態設計,它正在學生的出席和數據庫中動態填充我的設計。但是,每當有其他學生進來時,它並不會動態地反映這種變化。刷新正在發生,如果我點擊另一個日期,但我想要的是它每5秒或10秒刷新一次。即時通訊試圖研究堆棧中的答案,但所有的答案都不同於我的答案。如何刷新每5秒鐘內線程中的runOnUiThread?

+0

有沒有runOnUiThread(新的Runnable()在你的建議重複 – picolo

+0

我正在尋找的是刷新runOnUiThread – picolo

+0

是這個Android或其他東西嗎?Eclipse是用來開發各種Java,你需要告訴我們正是你正在使用的框架/ GUI –

回答

0

下面是如何每5秒鐘在線程中運行某些內容的示例,您可以將其添加到任意位置。

Handler handler = new Handler(Looper.getMainLooper()); 

Timer timer = new Timer(); 
timer.scheduleAtFixedRate(new TimerTask() { 
    public void run() { 
     data = bw.getAttendanceFromDB(term, course,sections,day); 
    } 
}, 0, 5 * 1000); 

handler.post(new Runnable() { 
    public void run() { 
     ArrayList<Properties> attendanceusers = attendanceUse(data); 
     addAttendance(attendanceusers); 
    } 
}) 

您可以將其從單擊監聽器中取出並運行一次。

當您的活動暫停時,請不要忘記撥打電話timer.cancel(),然後在恢復時重新運行()。