在下拉列表Spinner中添加自定義文字和圖標的方法。

有關於在下拉列表Spinner中添加自定義文字和圖標的方法,這裏以人的姓名和頭像,地址爲例,當然只要作出修改就可以把適配器應用於ListView 顯示。

 

首先創建一個儲存人信息的Person類。

public class Person {

    private String personName, personAddress;
    private Integer drawable;

    public Person(String personName,String personAddress,Integer drawable){
        super();
        this.personName = personName;
        this.personAddress = personAddress;
        this.drawable = drawable;
    }
    public String getPersonName(){
        return personName;
    }

    public void setPersonName(String personName){
        this.personName = personName;
    }

    public String getPersonAddress(){
        return personAddress;
    }

    public void setPersonAddress(String personAddress){
        this.personAddress = personAddress;
    }

    public Integer getDrawable() {
        return drawable;
    }

    public void setDrawable(Integer drawable) {
        this.drawable = drawable;
    }
}


然後創建一個MyAdapter類作爲適配器繼承自BaseAdapter,重寫必須的四個方法

public class MyAdapter extends BaseAdapter{

    private List<Person> mList = null;
    private Context mContext = null;

    public MyAdapter(Context pContext,List<Person> pList){
        this.mContext = pContext;
        this.mList = pList;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //利用getView方法給我們提供的convertView,也可以自己新建一個Layout
        if (convertView == null) {
            LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);     //佈局加載權限
            convertView = mLayoutInflater.inflate(R.layout.item_custom, null);
        }

        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        imageView.setImageResource(mList.get(position).getDrawable());

        TextView mTextView1 = (TextView) convertView.findViewById(R.id.textView1);
        TextView mTextView2 = (TextView) convertView.findViewById(R.id.textView2);

        mTextView1.setText(mList.get(position).getPersonName());
        mTextView2.setText(mList.get(position).getPersonAddress());

        return convertView;
    }
}



還有自定義的佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_0"
            android:scrollbarStyle="insideInset"
            android:id="@+id/imageView1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:layout_marginLeft="10dp"
            android:id="@+id/textView2"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</LinearLayout>


最後是Activity類的內容,這裏建立兩個Spinner作爲對比,一個是簡單使用系統的ArrayAdapter,另一個則是我們自定義的adapter


public class MainActivity extends ActionBarActivity {

    private Spinner spinner1 = null;
    private Spinner spinner2 = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        String[] mItems = getResources().getStringArray(R.array.language);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,android.R.layout.simple_spinner_item,mItems);      //綁定字符串數組
        spinner1.setAdapter(adapter);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setSelection(0, true);    //默認選中第一個
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] language = getResources().getStringArray(R.array.language);
                Toast.makeText(MainActivity.this, "你點擊的是:" + language[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        final List<Person> persons = new ArrayList<Person>();  //建立數據源
        persons.add(new Person("張三","北京",R.drawable.beijing));         //添加數據
        persons.add(new Person("李四", "上海",R.drawable.shanghai));
        persons.add(new Person("王五", "廣州",R.drawable.guangzhou));
        persons.add(new Person("趙六", "深圳",R.drawable.shenzhen));
        MyAdapter myAdapter = new MyAdapter(this,persons);     //實例化適配器

        spinner2.setAdapter(myAdapter);    //下拉列表綁定適配器
        spinner2.setSelection(0,true);    //默認選中第一個,避免打開程序就立即執行onItemSelected方法

        //設置點擊列表項目監聽事件
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "你點擊的是:" +
                        persons.get(position).getPersonName(), Toast.LENGTH_SHORT).show();
            }

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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章