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

I have ArrayList attendanceusers that populates my dynamic design, it is getting the attendance of students in the database and populating my design dynamically. However whenever another students come in, it doesn't reflect the change dynamically. The refresh is happening if im clicking another date, but what i want is for it to refresh for every 5 secs or 10 secs. im trying to research the answers here in stack but all the answers are different from mine.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
picolo
  • 41
  • 5

1 Answers1

0

Here's an example of how to run something in a thread every 5 seconds, you can add it wherever you want.

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

You can take that out of the click listener, and have it run once.

Don't forget to call timer.cancel() when your activity is paused, and then re-running that when it is resumed().

elmorabea
  • 3,243
  • 1
  • 14
  • 20