English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation and Example Code of Android Spinner and Adapter Pattern

Recently, I used Android Spinner in a project, and here I write a simple example to test how to use it.

Spinner

It is a dropdown list. Drag a Spinner control into the Android interface, set Android:entries=“@array/spinner_data”
Among which, spinner_data is an array set in the string. The values in the array are fixed. At this point, you can set up the dropdown list on the interface.

<Spinner 
    android:id="@"+id/spinner1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@"+id/textView1" 
    android:layout_below="@"+id/textView1" 
    android:layout_marginTop="17dp" 
    android:entries="@array/spinner_data"/> 

Of course, to improve user experience, you can set the attribute android:spinnerMode="dialog" so that the dropdown list has a pop-up box.

Adapter:

The adapter is mainly responsible for extracting data from the data source and passing it to various UI components. The main adapter is adapter, which includes ArrayAdapter and BaseAdapter. The adapter is responsible for obtaining data from a data source such as an array and passing it to a Spinner to display it on the interface.

Firstly, define the data source

private String[] ss=new String[]{ 
    "Cat", 
    "Tiger", 
    "Lion", 
  }; 

Data source2

private List<String> list=new ArrayList<String>(); 
list.add("百度"); 
    list.add("腾讯"); 
    list.add("阿里巴巴"); 

Secondly, define a class Myadapter that inherits from the abstract class BaseAdapter, with four abstract methods.

private class Myadapter extends BaseAdapter{ 
  @Override 
  public int getCount</span>() { 
    // TODO Auto-generated method stub 
    return ss.length; 
  } 
  @Override 
  public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
  } 
  @Override 
  public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
  } 
  @Override 
  public View getView</span>(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    TextView textView = new TextView(MainActivity.this); 
    textView.setText(ss[position]); 
    return textView; 
  } 

Then in the main function

Spinner spinner = (Spinner) findViewById(R.id.spinner);1); 
    BaseAdapter adapter = new Myadapter(); 
    spinner.setAdapter(adapter); 

First find the Spinner control and instantiate a new adapter. Through the adapter to grab the data source ss in the data.

The most important method of BaseAdapter adapter is getcount() and getview() method. The former returns the length of the data source, and the latter performs some operations.

Of course, you can use a simpler ArrayAdaper adapter.

Spinner spinner = (Spinner) findViewById(R.id.spinner);1); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss); 
    spinner.setAdapter(adapter); 

Through the above statements, you can directly locate in the array ss.

Thank you for reading, I hope it can help everyone. Thank you for your support of our website!

You May Also Like