package io.github.arpankapoor.verifymobilenumber;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.i18n.phonenumbers.PhoneNumberUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import io.github.arpankapoor.country.Country;

public class CountrySelector extends AppCompatActivity {

    private List<Country> getCountryList() {
        List<Country> countries = new ArrayList<>();

        Set<String> supportedRegions = PhoneNumberUtil.getInstance().getSupportedRegions();

        for (String countryIso : supportedRegions) {
            Country country = new Country(countryIso);
            countries.add(country);
        }

        Collections.sort(countries);
        return countries;
    }

    private void setListView() {
        ListView listView = (ListView) findViewById(R.id.listView);

        listView.setAdapter(new ArrayAdapter<>(
                this,
                R.layout.textview_country_list,
                getCountryList()
        ));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Country country = (Country) parent.getItemAtPosition(position);

                Intent output = new Intent();
                output.putExtra("country", country);
                setResult(RESULT_OK, output);
                finish();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country_selector);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setListView();
    }
}

@@ -75,16 +75,45 @@
 Intent;%0A
+import android.os.AsyncTask;%0A
 import a
@@ -130,16 +130,16 @@
 Bundle;%0A
-
 import a
@@ -1060,16 +1060,39 @@
 istView(
+List%3CCountry%3E countries
 ) %7B%0A    
@@ -1290,32 +1290,25 @@
         
-getC
+c
 ountr
-yList()
+ies
 %0A       
@@ -2144,16 +2144,20 @@
         
+new 
 setListV
@@ -2159,19 +2159,382 @@
 ListView
-();
+Task().execute();%0A    %7D%0A%0A    private class setListViewTask extends AsyncTask%3CVoid, Void, List%3CCountry%3E %3E %7B%0A%0A        @Override%0A        protected void onPostExecute(List%3CCountry%3E countries) %7B%0A            setListView(countries);%0A        %7D%0A%0A        @Override%0A        protected List%3CCountry%3E doInBackground(Void... params) %7B%0A            return getCountryList();%0A        %7D
 %0A    %7D%0A%7D

Populate country list in a background AsyncTask