package com.locovna.wmappy;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.locovna.wmappy.controller.CountryAdapter;
import com.locovna.wmappy.controller.QueryUtils;
import com.locovna.wmappy.model.Country;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
  public static final String LOG_TAG = MainActivity.class.getSimpleName();
  public static final String REQUEST_URL =
      "https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json";
  private CountryAdapter mAdapter;

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

    ListView countriesListView = (ListView) findViewById(R.id.list);

    mAdapter = new CountryAdapter(this, new ArrayList<Country>());
    countriesListView.setAdapter(mAdapter);

    countriesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        // Find the current country that was clicked on
        Country currentCountry = mAdapter.getItem(position);
        ArrayList cities = currentCountry.getCities();

        Toast.makeText(getApplicationContext(), "Cities of " + currentCountry.getCountry() + ": " + cities, Toast.LENGTH_LONG).show();
      }
    });

    CountryAsyncTask task = new CountryAsyncTask();
    task.execute(REQUEST_URL);
  }

  private class CountryAsyncTask extends AsyncTask<String, Void, List<Country>> {
    @Override
    protected List<Country> doInBackground(String... urls) {
      // Don't perform the request if there are no URLs, or the first URL is null
      if (urls.length < 1 || urls[0] == null) {
        return null;
      }

      List<Country> result = QueryUtils.fetchCountriesData(urls[0]);
      return result;
    }

    @Override
    protected void onPostExecute(List<Country> data) {
      // Clear the adapter of previous countries data
      mAdapter.clear();

      // If there is a valid list of {@link Countries}s, then add them to the adapter's
      // data set. This will trigger the ListView to update.
      if (data != null && !data.isEmpty()) {
        mAdapter.addAll(data);
      }
    }
  }
}

@@ -18,24 +18,96 @@
 na.wmappy;%0A%0A
+import android.app.AlertDialog;%0Aimport android.content.DialogInterface;%0A
 import andro
@@ -255,24 +255,60 @@
 dapterView;%0A
+import android.widget.ArrayAdapter;%0A
 import andro
@@ -1563,25 +1563,34 @@
 etCities();%0A
-%0A
+        //
         Toas
@@ -1708,24 +1708,52 @@
 NG).show();%0A
+        showDialog(cities);%0A
       %7D%0A    
@@ -1845,16 +1845,735 @@
 );%0A  %7D%0A%0A
+  private void showDialog(final ArrayList cities) %7B%0A    final ArrayAdapter%3CString%3E adapter = new ArrayAdapter%3CString%3E(MainActivity.this, android.R.layout.simple_list_item_1, cities);%0A    AlertDialog.Builder builder = new AlertDialog.Builder(this);%0A    builder.setTitle(%22Choose your city to explore%22)%0A        .setAdapter(adapter, new DialogInterface.OnClickListener() %7B%0A          @Override%0A          public void onClick(DialogInterface dialog, int id) %7B%0A            Object checkedItem = cities.toArray()%5Bid%5D.toString();%0A            Toast.makeText(getApplicationContext(), %22some action!%22 + checkedItem, Toast.LENGTH_LONG).show();%0A          %7D%0A        %7D);%0A    AlertDialog dialog = builder.create();%0A    dialog.show();%0A  %7D%0A%0A
   privat

Set up list of cities as dialog