title: Android : Thread not passing data to Handler
body: <p>I am working on an Android project in which I have to connect to a server, retrieve a list of restaurants and display it in a ListView. The situation is when the app is started, for testing, I am starting another Intent and running a thread inside it. Inside the thread, I am retrieving data from a server which I would like to display it on the ListView. But the handler method which should get the data and display it, is not working. 
  Unfortunately I am getting no errors to isolate and deal with the problem, and as passing data between threads is a core function, I am getting lost in the so many methods it calls in between. Kindly have a look what I am doing wrong.</p>
  
  <p>MainActivity class :</p>
  
  <pre><code>public class MyActivity extends Activity {
  
      RestaurantServiceImpl restaurantService = new RestaurantServiceImpl();
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          Button button = (Button) findViewById(R.id.standortermitteln);
  //Below starts the new view
          Intent listScreen = new Intent(getApplicationContext(),RestaurantList.class); 
  }
  </code></pre>
  
  <p>RestaurantList.java :</p>
  
  <pre><code>  public class RestaurantList extends Activity {
  
          String url1 = "http://192.168.178.40:8080/restaurant/listing";
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.restos);
  
              ListView listView = (ListView) findViewById(R.id.restaurantList);
              Thread thread = new Thread(new Runnable() {
  
                  @Override
                  public void run() {
                      Log.d("Are we in listRestaurants","Checking");
                      try {
                          RestTemplate restTemplate = new RestTemplate();
                          HttpHeaders requestHeaders = new HttpHeaders();
                          requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
                          HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
                          restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
                          ResponseEntity&lt;Restaurant[]&gt; responseEntity = restTemplate.exchange(url1, HttpMethod.GET, requestEntity, Restaurant[].class);
                          Restaurant[] restaurantList = responseEntity.getBody();
                      for(Restaurant restaurant : restaurantList){
  // I am able to print these messages,
                              Log.d(restaurant.getRestaurantName(), "Restaurant name");
                          }
                      } catch (Exception e) {
                          Log.d("We are in stacktrace",e.toString());
                          e.printStackTrace();
                      }
                  }
              });
              thread.setPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
              thread.start();
  
  // I am not reaching here to set the ListView
               Handler messageHandler = new Handler(){
                  public void handleMessage(Message message){
                      try {
                          Restaurant[] restaurants = (Restaurant[]) message.obj;
                          for(Restaurant restaurant : restaurants){
                              Log.d(restaurant.getRestaurantName(), "Restaurant name");
                          }
                          if(restaurants.length&lt;1){
                              Log.d("Restaurants from thread are empty"," check");
                          }
                          for(Restaurant restaurant : restaurants){
                              Log.d(restaurant.getRestaurantName(), "Restaurant name in messageHandler");
                          }
                          ArrayAdapter&lt;Restaurant&gt; adapter = new ArrayAdapter&lt;&gt;(getApplicationContext(),android.R.layout.simple_list_item_1,restaurants);
                          listView.setAdapter(adapter);
                      }catch (Exception e){
                          e.printStackTrace();
                      }
                  }
              };
          }
      }
  </code></pre>