package idv.samael.mrtapproachingstations;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

import com.google.gson.Gson;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

public class MainActivity extends AppCompatActivity {
    private TrainInfoAdapter mAdapter;
    private DataTaipeiService mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Bind list view.
        ListView trainList = (ListView) findViewById(R.id.list_mrt);
        mAdapter = new TrainInfoAdapter(this);
        if (trainList != null) {
            trainList.setAdapter(mAdapter);
        }

        // Create web service wrapper.
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://data.taipei")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mService = retrofit.create(DataTaipeiService.class);

        // Refresh immediately.
        refresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh:
                refresh();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

    private void refresh() {
        // Clear old data.
        mAdapter.getItems().clear();
        mAdapter.notifyDataSetChanged();

        // Update train info.
        Call<ApproachingTrains> trainsCall = mService.listApproachingTrains();
        trainsCall.enqueue(new Callback<ApproachingTrains>() {
            @Override
            public void onResponse(Response<ApproachingTrains> response, Retrofit retrofit) {

                // Fill the list view.
                for (ApproachingTrains.TrainInfo trainInfo : response.body().result.results) {
                    mAdapter.getItems().add(trainInfo);
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("Data.Taipei", t.getMessage(), t);
            }
        });
    }
}

@@ -258,16 +258,45 @@
 istView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -319,16 +319,40 @@
 .Gson;%0A%0A
+import java.util.List;%0A%0A
 import r
@@ -2245,17 +2245,415 @@
 ofit) %7B%0A
-%0A
+                ApproachingTrains responseBody;%0A                ApproachingTrains.ApproachingTrainsResult resultObject;%0A                List%3CApproachingTrains.TrainInfo%3E trainInfoList;%0A                if ((responseBody = response.body()) != null &&%0A                        (resultObject = responseBody.result) != null &&%0A                        (trainInfoList = resultObject.results) != null) %7B%0A    
         
@@ -2675,32 +2675,36 @@
  the list view.%0A
+    
                 
@@ -2794,32 +2794,36 @@
                 
+    
 mAdapter.getItem
@@ -2850,34 +2850,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -2874,32 +2874,34 @@
                 
+  
 mAdapter.notifyD
@@ -2913,24 +2913,42 @@
 tChanged();%0A
+                %7D%0A
             
@@ -3074,24 +3074,118 @@
 sage(), t);%0A
+                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();%0A
             

Update refresh() method.