Custom Spinner with key value pair Requirement
- if developer want to select item by ID not by Index of spinner.
- if developer want to get selected object's ID by Index of spinner.
Steps To Create Custom Spinner
- Create Custom Class Country
- Create XML for Spinner in layout/activity_main.xml
- Create CustomAdapter Class KeyValueSpinner & impliment SpinnerAdapter
- Create ArrayList
alCountry
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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="41dp" />
Step - 3
public class KeyValueSpinner implements SpinnerAdapter{
Context context;
ArrayList
public KeyValueSpinner(Context context ,ArrayList
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
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.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