Hello.

I can not figure out how to update the ListWiew by timer.

java.util.Timer timer2 = new java.util.Timer(); TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub rebutmsg(); } }; timer2.schedule(task, 10000); 

Here is the function that updates

 public void rebutmsg() { String s = (String) POST.rebutmsg(); String[] t = s.split("%"); int i; for (i = 0; i <= 59; i++) { hm = new HashMap < String, Object > (); hm.put(AUTHORKEY, t[i]); i++; hm.put(TEXTKEY, t[i]); i++; hm.put(TIMEKEY, t[i]); myBooks.add(hm); } Log.d("TIMER", " for(i=0; i<=59;i++){"); adapter = new SimpleAdapter( this, myBooks, R.layout.list, new String[] { AUTHORKEY, TEXTKEY, TIMEKEY }, new int[] { R.id.author, R.id.text, R.id.time } ); listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

}

Errors take off after the execution of this line

 adapter = new SimpleAdapter( this, myBooks, R.layout.list, new String[] { AUTHORKEY, TEXTKEY, TIMEKEY }, new int[] { R.id.author, R.id.text, R.id.time } ); listView.setAdapter(adapter); 

01-19 08:51:47.991: W/dalvikvm(5319): threadid=7: thread exiting with uncaught exception (group=0xb57de278) 01-19 08:51:47.991: E/AndroidRuntime(5319): FATAL EXCEPTION: Timer-0 01-19 08:51:47.991: E/AndroidRuntime(5319): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.view.ViewRoot.checkThread(ViewRoot.java:2802) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.view.View.invalidate(View.java:5139) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.widget.AbsListView.resetList(AbsListView.java:1011) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.widget.ListView.resetList(ListView.java:496) 01-19 08:51:47.991: E/AndroidRuntime(5319): at android.widget.ListView.setAdapter(ListView.java:425) 01-19 08:51:47.991: E/AndroidRuntime(5319): at com.fkn.chat.ListviewActivity.rebutmsg(ListviewActivity.java:114) 01-19 08:51:47.991: E/AndroidRuntime(5319): at com.fkn.chat.ListviewActivity$1.run(ListviewActivity.java:44) 01-19 08:51:47.991: E/AndroidRuntime(5319): at java.util.Timer$TimerImpl.run(Timer.java:289)

  • Solved the issue by adding this to the Timer run. Handler handler = new Handler (); handler.handler.post (new Runnable () {public void run () {// TODO Auto-generated method stub rebutmsg ();}}); But now another problem, does the timer work only once ?? Google is not helping. - tigr240172
  • I do not recommend using the timer, having a more versatile tool in the android - handler - kENNAAAAA
  • OK, then please advise me how can I update the listview, for example every 5 seconds or 10 seconds? - tigr240172

1 answer 1

Try this:

 private class Updater extends Thread { public boolean stopped = false; public void run() { try { while (!stopped) { // Активность списка SomeActivity.runOnUiThread( new Runnable() { @Override public void run() { // Обновление списка } } ); try { Thread.sleep(1000); } catch (Exception e) { } } } catch (Exception e) { } } } 

External flow control:

 Updater u = new Updater(); u.start(); 
  • Thank. It worked. Can you have one thing: SomeActivity.runOnUiThread (- emphasizes SomeActivity, what is it and what did I write for it instead (helped just remove it)? - tigr240172