Android - Configure Spinner to Use Array

Android - configure Spinner to use array

Rather than the dual array method, why not fill your ArrayAdapter programmatically with objects of a known type and use that. I've written a tutorial of a similar nature (link at the bottom) that does this. The basic premise is to create an array of Java objects, tell the spinner about the, and then use those objects directly from the spinner class. In my example I have an object representing a "State" which is defined as follows:

package com.katr.spinnerdemo;

public class State {

// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";

// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
id = _id;
name = _name;
abbrev = _abbrev;
}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control. If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
return( name + " (" + abbrev + ")" );
}
}

Then you can populate a spinner with an array of these classes as follows:

       // Step 1: Locate our spinner control and save it to the class for convenience
// You could get it every time, I'm just being lazy... :-)
spinner = (Spinner)this.findViewById(R.id.Spinner01);

// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new State[] {
new State( 1, "Minnesota", "MN" ),
new State( 99, "Wisconsin", "WI" ),
new State( 53, "Utah", "UT" ),
new State( 153, "Texas", "TX" )
});

// Step 3: Tell the spinner about our adapter
spinner.setAdapter(spinnerArrayAdapter);

You can retrieve the selected item as follows:

State st = (State)spinner.getSelectedItem();

And now you have a bona fide Java class to work with. If you want to intercept when the spinner value changes, just implement the OnItemSelectedListener and add the appropriate methods to handle the events.

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
// Get the currently selected State object from the spinner
State st = (State)spinner.getSelectedItem();

// Now do something with it.
}

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

You can find the whole tutorial here:
http://www.katr.com/article_android_spinner01.php

Android: Create spinner programmatically from array

ArrayAdapter<String> should work.

i.e.:

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,
spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

How to change the array source for a spinner? (Android Studio)

Try this

        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
spinner1 = (Spinner)findViewById(R.id.spinner1);

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
case R.id.button_1:
a1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item ,tickets);
spinner1.setAdapter(a1);
break;

case R.id.button_2:
a1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,subscriptions);
spinner1.setAdapter(a1);
break;

}

a1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

}
});

spinner1.setOnItemSelectedListener(this);

Note you need to declare ArrayAdapter a1;

a simple way to declare a spinner with array

As per my understanding of your requirement I have done below coding, so please check it, hope it will help you.

First Implement Listner in your Activity

    public class MainActivity  extends Activity implements AdapterView.OnItemSelectedListener

Now Your XML file should look like below

activity_main.xml

    <LinearLayout
android:id="@+id/main_Layout"
android:background="#FF0000"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
android:id="@+id/scMainView"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/llSpinnerSection"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
</LinearLayout>

Now in your main activity declare your layout as below

   LinearLayout  mainSpinnerLayout = (LinearLayout) findViewById(R.id.llSpinnerSection);

Now Create One Method For Getting Data And Set Spinner

   public void createSpinners()
{

final DataHelper db = new DataHelper(getApplicationContext());
final List<String> labelKolom = db.getKolom();

spnKolom = new Spinner[labelKolom.size()];

for (int itung=0;itung<labelKolom.size();itung++)
{
spnKolom[itung] = new Spinner(this);
spnKolom[itung].setTag(itung);

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labelKolom);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnKolom[itung].setAdapter(spinnerArrayAdapter);
spnKolom[itung].setOnItemSelectedListener(this);

if(itung == 0)
{
spnKolom[itung].setVisibility(View.VISIBLE);
}
else
{
spnKolom[itung].setVisibility(View.GONE);
}

mainSpinnerLayout.addView(spnKolom[itung]);
}
}

And Finally Implement Methods For Spinner To Show Next Spinner on Item Selection

//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {

if(position < spnKolom.length - 1)
{
spnKolom[position + 1].setVisibility(View.VISIBLE);
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

Hope this will work for you

Setting the drop down list of a spinner with an int[] array

use below code

  String[] array = {"1", "2","3", "4","5","6","7","8","9","10"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item, array);

instead of

  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
intArray, android.R.layout.simple_spinner_item);

How to use spinner and fill it from array in android

first create a xml in string for your String array

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>

then use the above xml for string array in your code like below

   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

Create a spinner using a double array

You are using the ArrayAdapter.createFromResource() wrong. Please, refer to the documentation.

To fix your problem, add your FULL array to an array in values > string.xml

<array name="spinner_val">
<item>"2.0"</item>
<item>"3.0"</item>
<item>"4.0"</item>
...
</array>

Then retrieve them as strings since you are going to display them in a text.

ArrayAdapter.createFromResource (this, 
getContext().getResources().getStringArray(R.array.spinner_val),
R.layout.simple_selectable_list_item);

Use string array to populate Spinner

Simplest way to bind ListView and Spinner control with String Array is

android:entries = "@array/nameofarray"

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MainSpinner"
tools:listitem="@layout/support_simple_spinner_dropdown_item"
android:entries="@array/Convert_Type"/>

If you want to change the theme of each item of Spinner then put below style into res/values/styles.xml

<style name="ItemTextAppearance">
<item name="android:textColor">#f00</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">monospace</item>
</style>

and the set

android:theme="@style/ItemTextAppearance"

of spinner.



Related Topics



Leave a reply



Submit