Pages

Saturday, August 17, 2013

Custom Spinner with Key Value

Custom Spinner with key value pair Requirement

  1. if developer want to select item by ID not by Index of spinner.
  2. if developer want to get selected object's ID by Index of spinner.

Steps To Create Custom Spinner

  1. Create Custom Class Country
  2. Create XML for Spinner in layout/activity_main.xml
  3. Create CustomAdapter Class KeyValueSpinner & impliment SpinnerAdapter
  4. Create ArrayList alCountry
You can create Custom Spinner as follows:-

Step-1

public class Country {
    int ID;
    String CountryName;

    public Country(int ID,String CountryName) {
        this.ID = ID;
        this.CountryName = CountryName;
    }

    public String getCountryName() {
        return CountryName;
    }
    public void setCountryName(String countryName) {
        CountryName = countryName;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
}

Step-2 layout/activity_main.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

            android:id="@+id/spinCountry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp" />


Step - 3

public class KeyValueSpinner implements SpinnerAdapter{
    Context context;
    ArrayList alCountry;
   
    public KeyValueSpinner(Context context ,ArrayList alCountry){
        this.context =context;
        this.alCountry = alCountry;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return alCountry.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return alCountry.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
//Note:-Create this two method
getIDFromIndex and getIndexByID
    public int getIDFromIndex(int Index) {
        return    alCountry.get(Index).getID();      
    }
   
    public int getIndexByID(int ID) {
        for(int i=0;i            if(alCountry.get(i).getID() == ID){
                return i;
            }
        }
        return -1;
    }
   
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView textview = (TextView) inflater.inflate(android.R.layout.simple_spinner_item, null);
        textview.setText(alCountry.get(position).getCountryName());
      
        return textview;
    }

    @Override
    public int getViewTypeCount() {
        return android.R.layout.simple_spinner_item;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView textview = (TextView) inflater.inflate(android.R.layout.simple_spinner_item, null);
        textview.setText(alCountry.get(position).getCountryName());
      
        return textview;
    }
}



Step - 4

public class MainActivity extends Activity {
    Spinner spin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spin = (Spinner) findViewById(R.id.spinCountry);
        ArrayList alCountry = new ArrayList();
        alCountry.add(new Country(1, "India"));
        alCountry.add(new Country(6, "Australia"));
        alCountry.add(new Country(8, "US"));
        alCountry.add(new Country(3, "Canada"));
        alCountry.add(new Country(5, "China"));
       
        KeyValueSpinner adapter = new KeyValueSpinner(MainActivity.this, alCountry);
        spin.setAdapter(adapter);
    }
}

Note:- Developer can use this two methods  adapter.getIDFromIndex(index) and adapter.getIndexByID(ID) to get ID from Index and get Index From ID
 

No comments:

Post a Comment

Must Comment