AdapterView - How to get the selected item
In this tutorial you will learn about the
getSelectedItem
method which you can use to get the selected item in anAdapterView
in Android.
This tutorial will help you learn the following concepts:
- How to bind data to
Spinner
widget. - How to set the
DropdownView
resource. - How to get the selected item from a
Spinner
.
Example 1: Android AdapterView getSelectedItem Example.
Learn how to get the selected item from a
Spinner
using thegetSelectedItem()
method in Android.
This example will comprise the following files:
MainActivity.java
Step 1: Create Project
- Open your
AndroidStudio
IDE. - Go to
File-->New-->Project
to create a new project.
Step 2: Add Dependencies
Step 3: Design Layouts
*(a). activity_main.xml
Create a file named activity_main.xml
and design it as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1_text"/>
"
<Spinner android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Step 4: Write Code
Write Code as follows:
*(a). MainActivity.java
Create a file named MainActivity.java
Add imports
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
Extend the Activity
class to create our Activity
:
Prepare a Context as an instance field:
Override the onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Assign the Context to this:
Instantiate an ArrayAdapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
Because we are using the Spinner
widget. set the DropdownView resource to android.R.layout.simple_spinner_dropdown_item
:
Add items to the adapter
Reference our button1:
Listen to its click event:
Get the selected item from a Spinner
using the getSelectedItem()
method:
You can then show it in a Toast
message
Here is the full code
package com.bgstation0.android.sample.adapterview_;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
//end
public class MainActivity extends Activity {
//end
Context mContext;
//end
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//end
mContext = this;
//end
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
//end
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//end
adapter.add("Item1");
adapter.add("item2");
adapter.add("Item3");
//end
// -Reference the `Spinner`:
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
//end
spinner.setAdapter(adapter);
//end
Button button1 = (Button)findViewById(R.id.button1);
//end
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//end
String itemStr = (String)spinner.getSelectedItem();
//end
Toast.makeText(mContext, itemStr, Toast.LENGTH_LONG).show();
//end
}
});
}
}
Run
Simply copy the source code into your Android Project,Build and Run.
Example 2: Set and Get Selected Item
This tutorial will help you learn the following concepts:
- How to set the selected item listener on a spinner.
- How to get the selected item on a spinner.
This example will comprise the following files:
MainActivity.java
Step 1: Create Project
- Open your
AndroidStudio
IDE. - Go to
File-->New-->Project
to create a new project.
Step 2: Dependencies
No special dependencies are needed.
Step 3: Design Layouts
*(a). activity_main.xml
Create a file named activity_main.xml
and design it as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1_text"/>
"
<Spinner android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Step 4: Write Code
Write Code as follows:
*(a). MainActivity.java
Create a file named MainActivity.java
Create our Activity
Define a Context
as our instance field:
Override the onCreate()
function:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Create an ArrayAdapter and set its layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); // android.R.layout.simple_spinner_itemŃA_v^쐬.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // hbv_EXg\̃r[͊android.R.layout.simple_spinner_dropdown_itemZbg.
Add items to the adapter
Reference the spinner and set its adapter
Reference a button and set its click listener:
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Get and show the selected item
String itemStr = (String)spinner.getSelectedItem(); // AdapterView.getSelectedItemőIꂽACeitemStr擾.
Toast.makeText(mContext, itemStr, Toast.LENGTH_LONG).show(); // itemStr
Invoke the setOnItemSelectedListener
on our Spinner
. This will allow us listen to selection events.
Override the onItemSelected
:
Get the parent spinner:
then get the selected item using the getSelectedItem()
function and show it in a message:
String itemStr = (String)spnr.getSelectedItem();
Toast.makeText(mContext, itemStr, Toast.LENGTH_LONG).show(); // itemStr\.
You can also override the onNothingSelected()
selected callback
Here is the full code
package com.bgstation0.android.sample.adapterview_;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
//end
Context mContext;
//end
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
//end
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); // android.R.layout.simple_spinner_itemŃA_v^쐬.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // hbv_EXg\̃r[͊android.R.layout.simple_spinner_dropdown_itemZbg.
//end
adapter.add("Item1");
adapter.add("item2");
adapter.add("Item3");
//end
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(adapter);
//end
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//end
String itemStr = (String)spinner.getSelectedItem(); // AdapterView.getSelectedItemőIꂽACeitemStr擾.
Toast.makeText(mContext, itemStr, Toast.LENGTH_LONG).show(); // itemStr
//end
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//end
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//end
Spinner spnr = (Spinner)parent;
//end
String itemStr = (String)spnr.getSelectedItem();
Toast.makeText(mContext, itemStr, Toast.LENGTH_LONG).show(); // itemStr\.
//end
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
//end
});
}
}
Run
Simply copy the source code into your Android Project, Build and Run.