title: Android: Updating gridview multiple times after x seconds
body: <p>I am having a problem updating the view in android every x seconds.</p>
  
  <p>To get to know how android works I am writing a small game. 
  It has a GameController which holds the game loop. Inside the loop, the logic is executed and afterwards the gui will be informed about the changes.</p>
  
  <p>The problem is that the changes cannot be seen in the view. The view stays the same until the game loop finishes and updates then only once.</p>
  
  <p>Here is my code (the important parts):</p>
  
  <p>GameController.java:</p>
  
  <pre><code>IGui gui;
  
  public void play() {
      while (playing) {
          getAndProcessInput();
          updateGui();
          try {
              Thread.sleep(500);
          } catch (InterruptedException ex) {
          }
      }
  }
  
  private void updateGui() {
      gui.setPlayer(x, y);
      // ...
  }
  </code></pre>
  
  <p>GameActivity.java:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      GridView gridview = (GridView) findViewById(R.id.GridView1);
      TextAdapter = new TextAdapter(this);
      gridview.setAdapter(textAdapter);
  
      GameController c = new GameController();
  
      // here, the game loop shoud start.
      // what i tried:
  
      // new Thread(c).start();   &lt;-- causes crash
  
      // c.play();     &lt;-- causes view to frease until game loop is done
  
      this.runOnUiThread(c);  &lt;-- causes view to frease until game loop is done
  }
  </code></pre>
  
  <p>TextAdapter.java:</p>
  
  <pre><code>public class TextAdapter extends BaseAdapter implements IGui {
  
      private final Context context;
      private final String[] texts;
  
      public TextAdapter(Context context) {
          this.context = context;
          texts = new String[height * width];
          for (int i = 0; i &lt; texts.length; i++) {
              texts[i] = " ";
          }
      }
  
      public int getCount() {
          return height * width;
      }
  
      public Object getItem(int position) {
          return null;
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          TextView tv;
          if (convertView == null) {
              tv = new TextView(context);
              tv.setLayoutParams(new GridView.LayoutParams(25, 25));
          } else {
              tv = (TextView) convertView;
          }
  
          tv.setText(texts[position]);
          return tv; // &lt;-- this happens only when game loop is done, not everytime something changed
      }
  
      @Override 
      public void setPlayer(int x, int y) {
          texts[width * y + x] = "X";
          // this.notifyDataSetChanged();  &lt;-- this does not help (view still freases) 
          //                                   gridview.invalidateViews(); does not help either
      }
  }
  </code></pre>
  
  <p>I googled a lot and tried a lot as well (and I do know that similar questions where asked here already, but they did not help me either), but somehow it just does not work. 
  I cannot get it do work that the view and logic run on different theads in android, and if they run on the same thread the logic blocks the view.
  Any help would be greatly appreciated.</p>
  
  <p>// edit:</p>
  
  <p>If I try new Thread(c).start(); LogCat sais:
  java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()</p>
  
  <p>And if I add Looper.prepare(); :
  java.lang.RuntimeException: Unable to start activity ComponentInfo{GameActivity}: java.lang.RuntimeException: Only one Looper may be created per thread</p>
  
  <p>If I try this.runOnUiThread(c); there are no errors.</p>