Android Open Source - android-spinnerwheel Cities Activity






From Project

Back to project page android-spinnerwheel.

License

The source code is released under:

Apache License

If you think the Android project android-spinnerwheel listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package antistatic.spinnerwheel.demo;
//  ww w  .  java  2 s .c om
import antistatic.spinnerwheel.AbstractWheel;
import antistatic.spinnerwheel.OnWheelChangedListener;
import antistatic.spinnerwheel.OnWheelScrollListener;
import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter;
import antistatic.spinnerwheel.adapters.ArrayWheelAdapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class CitiesActivity extends Activity {
    // Scrolling flag
    private boolean scrolling = false;

    private int mActiveCities[] = new int[] {
            1, 1, 1, 1
    };
    
    private int mActiveCountry;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.repopulating_data);
                
        final AbstractWheel country = (AbstractWheel) findViewById(R.id.country);
        country.setVisibleItems(3);
        country.setViewAdapter(new CountryAdapter(this));

        final String cities[][] = new String[][] {
                new String[] {"New York", "Washington", "Chicago", "Atlanta", "Orlando", "Los Angeles", "Houston", "New Orleans"},
                new String[] {"Ottawa", "Vancouver", "Toronto", "Windsor", "Montreal", "Calgary", "Winnipeg", "Edmonton"},
                new String[] {"Kyiv", "Simferopol", "Lviv", "Kharkiv", "Odessa", "Mariupol", "Lugansk", "Sevastopol"},
                new String[] {"Paris", "Bordeaux", "Le Mans", "Orleans", "Valence", "Amiens", "Rouen", "Touluse", "La Rochelle"},
        };


        final AbstractWheel city = (AbstractWheel) findViewById(R.id.city);
        city.setVisibleItems(5);

        city.addChangingListener(new OnWheelChangedListener() {
            public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
                if (!scrolling) {
                    mActiveCities[mActiveCountry] = newValue;
                }
            }
        });

        country.addChangingListener(new OnWheelChangedListener() {
            public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
                if (!scrolling) {
                    updateCities(city, cities, newValue);
                }
            }
        });
        
        country.addScrollingListener( new OnWheelScrollListener() {
            public void onScrollingStarted(AbstractWheel wheel) {
                scrolling = true;
            }
            public void onScrollingFinished(AbstractWheel wheel) {
                scrolling = false;
                updateCities(city, cities, country.getCurrentItem());
            }
        });

        country.setCurrentItem(1);
    }
    
    /**
     * Updates the city spinnerwheel
     */
    private void updateCities(AbstractWheel city, String cities[][], int index) {
        mActiveCountry = index;
        ArrayWheelAdapter<String> adapter =
            new ArrayWheelAdapter<String>(this, cities[index]);
        adapter.setTextSize(18);
        city.setViewAdapter(adapter);
        city.setCurrentItem(mActiveCities[index]);
    }
    
    /**
     * Adapter for countries
     */
    private class CountryAdapter extends AbstractWheelTextAdapter {
        // Countries names
        private String countries[] =
            new String[] {"USA", "Canada", "Ukraine", "France"};
        // Countries flags
        private int flags[] =
            new int[] {R.drawable.usa, R.drawable.canada, R.drawable.ukraine, R.drawable.france};
        
        /**
         * Constructor
         */
        protected CountryAdapter(Context context) {
            super(context, R.layout.country_item, NO_RESOURCE);
            
            setItemTextResource(R.id.country_name);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            ImageView img = (ImageView) view.findViewById(R.id.flag);
            img.setImageResource(flags[index]);
            return view;
        }
        
        @Override
        public int getItemsCount() {
            return countries.length;
        }
        
        @Override
        protected CharSequence getItemText(int index) {
            return countries[index];
        }
    }
}




Java Source Code List

antistatic.spinnerwheel.AbstractWheelView.java
antistatic.spinnerwheel.AbstractWheel.java
antistatic.spinnerwheel.ItemsRange.java
antistatic.spinnerwheel.OnWheelChangedListener.java
antistatic.spinnerwheel.OnWheelClickedListener.java
antistatic.spinnerwheel.OnWheelScrollListener.java
antistatic.spinnerwheel.WheelHorizontalScroller.java
antistatic.spinnerwheel.WheelHorizontalView.java
antistatic.spinnerwheel.WheelRecycler.java
antistatic.spinnerwheel.WheelScroller.java
antistatic.spinnerwheel.WheelVerticalScroller.java
antistatic.spinnerwheel.WheelVerticalView.java
antistatic.spinnerwheel.adapters.AbstractWheelAdapter.java
antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter.java
antistatic.spinnerwheel.adapters.ArrayWheelAdapter.java
antistatic.spinnerwheel.adapters.NumericWheelAdapter.java
antistatic.spinnerwheel.adapters.WheelViewAdapter.java
antistatic.spinnerwheel.demo.CitiesActivity.java
antistatic.spinnerwheel.demo.ProgrammaticSwitchingActivity.java
antistatic.spinnerwheel.demo.TimePickerActivity.java
antistatic.spinnerwheel.demo.TimePickerCustomViewsActivity.java
antistatic.spinnerwheel.demo.WheelDemo.java