Skip to main content

Spinner with different display text and return value

In the most basic Spinner implementation, selected item can be retrieved by calling parent.getItemAtPosition(position) in onItemSelected() method in OnItemSelectedListener. It will be the same object of the display items, as show in the spinner0 of the example.

http://android-er.blogspot.in/2014/04/spinner-with-different-displat-text-and.html

Sometimes, we want to display some meaningful text in Spinner (such as "Sunday", "Monday"...), but return some other value when any item selected (such as 0, 2...).

Spinner with different display text and return value
Here I show two approaches:
  • The first one may be the simplest method, spinner1 in the example. Create another array to hold the values we want to return. And return the coresponding item on position in onItemSelected().
  • The second approach implement our custom class to hold the display text and the return value. And implement our custom Adapter, as shown in spinner2 in the example.

package com.example.androidspinnertext;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 String[] text0 = { "Sunday", "Monday", "Tuesday", 
   "Wednesday", "Thursday", "Friday", "Saturday" };
 
 String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY", 
   "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
 int[] val1 = { 0, 1, 2, 3, 4, 5, 6};
 
 MyClass[] obj2 ={
  new MyClass("SUN", 0),
  new MyClass("MON", 1),
  new MyClass("TUE", 2),
  new MyClass("WED", 3),
  new MyClass("THU", 4),
  new MyClass("FRI", 5),
  new MyClass("SAT", 6)
 };
 
 Spinner spinner0, spinner1, spinner2;
 TextView textView0, textView1, textView2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textView0 = (TextView)findViewById(R.id.text0);
  spinner0 = (Spinner)findViewById(R.id.spinner0);
  ArrayAdapter<String> adapter0 = 
   new ArrayAdapter<String>(MainActivity.this, 
     android.R.layout.simple_spinner_item, text0);
  adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner0.setAdapter(adapter0);
  spinner0.setOnItemSelectedListener(onItemSelectedListener0);
  
  textView1 = (TextView)findViewById(R.id.text1);
  spinner1 = (Spinner)findViewById(R.id.spinner1);
  ArrayAdapter<String> adapter1 = 
   new ArrayAdapter<String>(MainActivity.this, 
     android.R.layout.simple_spinner_item, text1);
  adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner1.setAdapter(adapter1);
  spinner1.setOnItemSelectedListener(onItemSelectedListener1);
  
  textView2 = (TextView)findViewById(R.id.text2);
  spinner2 = (Spinner)findViewById(R.id.spinner2);
  MySpinnerAdapter adapter2 = 
   new MySpinnerAdapter(MainActivity.this, 
     android.R.layout.simple_spinner_item, obj2);
  //adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner2.setAdapter(adapter2);
  spinner2.setOnItemSelectedListener(onItemSelectedListener2);

 }
 
 OnItemSelectedListener onItemSelectedListener0 =
  new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    String s0 = (String)parent.getItemAtPosition(position);
    textView0.setText(s0);
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {}
 };
 
 OnItemSelectedListener onItemSelectedListener1 =
  new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    String s1 = String.valueOf(val1[position]);
    textView1.setText(s1);
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {}

 };
 
 OnItemSelectedListener onItemSelectedListener2 =
  new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    MyClass obj = (MyClass)(parent.getItemAtPosition(position));
    textView2.setText(String.valueOf(obj.getValue()));
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {}

 };
 
 //define our custom class
 public class MyClass{

  private String text;
     private int value;
     

     public MyClass(String text, int value){
      this.text = text;
      this.value = value;
     }
     
     public void setText(String text){
         this.text = text;
     }

     public String getText(){
         return this.text;
     }

     public void setValue(int value){
         this.value = value;
     }

     public int getValue(){
         return this.value;
     }
 }
 
 //custom adapter
 public class MySpinnerAdapter extends ArrayAdapter<MyClass>{

     private Context context;
     private MyClass[] myObjs;

     public MySpinnerAdapter(Context context, int textViewResourceId,
       MyClass[] myObjs) {
         super(context, textViewResourceId, myObjs);
         this.context = context;
         this.myObjs = myObjs;
     }

     public int getCount(){
        return myObjs.length;
     }

     public MyClass getItem(int position){
        return myObjs[position];
     }

     public long getItemId(int position){
        return position;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         TextView label = new TextView(context);
         label.setText(myObjs[position].getText());
         return label;
     }

     @Override
     public View getDropDownView(int position, View convertView,
             ViewGroup parent) {
         TextView label = new TextView(context);
         label.setText(myObjs[position].getText());
         return label;
     }
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidspinnertext.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/spinner0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
</LinearLayout>



download filesDownload the files.

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap then to bytes. Also

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App §   2.4.7  Set the color §   2.4.8  Adding Animations o    2.5  To plot grouped bar chart §   2.5.1  Creating Dataset o    2.6  Get the best of Android plus exclusive deals and freebies in your inbox!