note 30- Spinner

A.  default spinner :

1. must set a string-array in the string.xml

2. use function : ArrayAdpater. createFromResource(Context context, int strArrayId,                                          int spinnerNotExtantLayout);

     The itemLayout must be the     android.R.simple_spinner_item ;

4. adapter.setDropDownViewResource(int everyItemLayout) ;

    The everyItemLayout must be the android.R.simple_spinner_dropdown _item;

5. set the OnItemSelectedListener;

B. custom spinner

1. create an ArrayList . The list item can be ANY OBJECT , just override the toString function.

2. new an ArrayAdapter(Context context , int everyItemView,  int  everyItemTextId,  list  dataList);

    the custom view MUST BE WITH A TextView

3.  set the OnItemSelectedListener;

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, SpinnerTest"
    />
    
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/textViewId"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">SpinnerTest</string>
    <string-array name="planet_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>

 

package com.spinner_test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;

public class SpinnerTest extends Activity
{
    
    Spinner spinner;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        spinner=(Spinner)this.findViewById(R.id.spinner);
        
        
        
        
        
        //default create
//        ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(
//                this, 
//                R.array.planet_array, 
//                android.R.layout.simple_spinner_item);
//        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        
        
        
        
        
        //dynamic create
        
        List<String> list=new ArrayList<String>();
        list.add("test1");
        list.add("test2");
        
        //Context context
        //int everyItemView
        //int everyItemTextId
        //list dataList
        
        //list can contain any Object, just rewrite the toString;
        ArrayAdapter adapter=new ArrayAdapter(
                this,
                R.layout.item,
                R.id.textViewId,
                list);
        
        
        
        
        spinner.setAdapter(adapter);
        spinner.setPrompt("solar system");
        
        spinner.setOnItemSelectedListener(spinnerSelectListener);
        
        
    }
    
    
    OnItemSelectedListener spinnerSelectListener=new OnItemSelectedListener(){

        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
//            throw new UnsupportedOperationException("Not supported yet.");
            
            String selected=adapterView.getItemAtPosition(position).toString();
            Log.i("l","selected:"+selected);
        }

        public void onNothingSelected(AdapterView<?> adapterView) {
//            throw new UnsupportedOperationException("Not supported yet.");
            Log.i("l","nothing selected");
            
            
        }
        
    };
}

相关推荐