首页 > 代码库 > 20 UI_常用组件之 Spinner与适配器模式

20 UI_常用组件之 Spinner与适配器模式

UI_常用组件之 Spinner(下拉列表)与适配器模式

    <Spinner

        android:id="@+id/spinner1"

        android:layout_width="142dp"

        android:layout_height="wrap_content" 

        android:entries="@array/spinner_data"

        android:spinnerMode="dropdown"/>


 

下拉列表的显示方式:

直接下拉列表       android:spinnerMode="dropdown"

弹出式下拉列表     android:spinnerMode="dialog"

  


 

静态添加数据的方式:(此方式适用于静态数据,写入之后就不能改变)

main.xml中的Spinner写入:

android:entries="@array/spinner_data"

strings.xml中写入:

<string-array name="spinner_data">

        <item >陕西</item>

        <item >河北</item>

        <item >四川</item>

</string-array>

 

Adapter:(适配器模式)
       计算机编程中,适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。

BaseAdapter

       AdapterAndroid应用程序中起着非常重要的作用,应用也非常广泛,它可看作是数据源和UI组件之间的桥梁,其中Adapter、数据和UI之间的关系,BaseAdapterAndroid应用程序中经常用到的基础数据适配器,它的主要用途是将一组数据传到像ListViewSpinnerGalleryGridViewUI显示组件,它是继承自接口类Adapter

 


 

动态添加数据的方式:

1、添加数据源1的数据:

代码:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
    <!-- android:spinnerMode="dialog" 弹出式下拉列表-->
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="142dp"
        android:layout_height="wrap_content" 
        android:entries="@array/spinner_data"
        android:spinnerMode="dropdown"/>
</LinearLayout>

Test_spinnerActivity.java:

package test.spinner;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Test_spinnerActivity extends Activity {
    //数据源1
    private String[] ss = new String[]{
            "北京",
            "上海",
            "深圳"
    };
    private List<String> list = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //数据源2
        list.add("java");
        list.add("c++");
        list.add("php");
        
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        BaseAdapter adapter =new MyAdapter();
        spinner.setAdapter(adapter);
        //spinner-->adapter -->数据
    }
    
    private class MyAdapter extends BaseAdapter{
        public int getCount() {
            return ss.length;
        }
        public Object getItem(int arg0) {
            return null;
        }
        public long getItemId(int arg0) {
            return 0;
        }
        //通过count来调用getView,count是几就调用几次getView
        public View getView(int position, View converView, ViewGroup parent) {
            TextView textView = new TextView(Test_spinnerActivity.this);
            textView.setText(ss[position]);
            return textView;
        }
    }
}

运行结果:

技术分享

2、添加数据源2的数据:

代码:

main.xml:

  和上面去数据源1的代码一样

Test_spinnerActivity.java:

(黄色的底色的是修改的代码)

package test.spinner;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Test_spinnerActivity extends Activity {
    //数据源1
    private String[] ss = new String[]{
            "北京",
            "上海",
            "深圳"
    };
    private List<String> list = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //数据源2
        list.add("java");
        list.add("c++");
        list.add("php");
        
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        BaseAdapter adapter =new MyAdapter();
        spinner.setAdapter(adapter);
        //spinner-->adapter -->数据
    }
    
    private class MyAdapter extends BaseAdapter{
        public int getCount() {
            return list.size();
        }
        public Object getItem(int arg0) {
            return null;
        }
        public long getItemId(int arg0) {
            return 0;
        }
        //通过count来调用getView,count是几就调用几次getView
        public View getView(int position, View converView, ViewGroup parent) {
            TextView textView = new TextView(Test_spinnerActivity.this);
            textView.setText(list.get(position));
            return textView;
        }
    }
}

运行结果:

技术分享


 

ArrayAdapter

<String>  adapter=

new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);

spinner.setAdapter(adapter);

ss指一个字符串数组

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:spinnerMode="dialog"/>

</LinearLayout>

Test_spinnerActivity.java:

package test.spinner02;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class Test_spinner02Activity extends Activity {
    
    private String[] ss = new String[]{
            "北京",
            "上海",
            "深圳"
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
        
        spinner.setAdapter(adapter);
    }
}

运行结果:

技术分享


 

Spinner的监听器:

main.xml:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
    <!-- android:spinnerMode="dialog" 弹出式下拉列表-->
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="142dp"
        android:layout_height="wrap_content" 
        android:entries="@array/spinner_data"
        android:spinnerMode="dropdown"/>
</LinearLayout>

Test_spinnerActivity.java:

package test.spinner;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Test_spinnerActivity extends Activity {
    //数据源1
    private String[] ss = new String[]{
            "北京",
            "上海",
            "深圳"
    };
    private List<String> list = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //数据源2
        list.add("java");
        list.add("c++");
        list.add("php");
        
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        BaseAdapter adapter =new MyAdapter();
        spinner.setAdapter(adapter);
        
        spinner.setOnItemClickListener(new OnItemClickListener(){

            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                //position 被选中的item,相对应的数组索引
                Toast.makeText(Test_spinnerActivity.this, list.get(position), 0).show();
                
            }
            public void onNothingSelected(AdapterView<?> arg0){
                
            }
            
        });
        //spinner-->adapter -->数据
    }
    
    private class MyAdapter extends BaseAdapter{
        public int getCount() {
            return list.size();
        }
        public Object getItem(int arg0) {
            return null;
        }
        public long getItemId(int arg0) {
            return 0;
        }
        //通过count来调用getView,count是几就调用几次getView
        public View getView(int position, View converView, ViewGroup parent) {
            TextView textView = new TextView(Test_spinnerActivity.this);
            textView.setText(list.get(position));
            return textView;
        }
    }
}

运行结果:

技术分享

 

20 UI_常用组件之 Spinner与适配器模式