ListView中比較簡單但又很是方便的ArrayAdapter。java
ArrayAdapter是BaseAdapter的派生類,在BaseAdapter的基礎上,添加了一項重大的功能:能夠直接使用泛型構造。android
先來看一個簡單的例子:數組
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) this.findViewById(R.id.list); UserAdapter adapter = new UserAdapter(this, R.layout.list_item); adapter.add(new User(10, "小智", "男")); adapter.add(new User(10, "小霞", "女")); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class UserAdapter extends ArrayAdapter<User> { private int mResourceId; public UserAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); this.mResourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { User user = getItem(position); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(mResourceId, null); TextView nameText = (TextView) view.findViewById(R.id.name); TextView ageText = (TextView) view.findViewById(R.id.age); TextView sexText = (TextView) view.findViewById(R.id.sex); nameText.setText(user.getName()); ageText.setText(user.getAge()); sexText.setText(user.getSex()); return view; } } class User { private int mAge; private String mName; private String mSex; public User(int age, String name, String sex) { this.mAge = age; this.mName = name; this.mSex = sex; } public String getName() { return this.mName; } public String getAge() { return this.mAge + ""; } public String getSex() { return this.mSex; } }
這裏自定義了一個ArrayAdapter,有關於Adapter的使用在以前的SimpleAdapter中已經涉及到了,因此這裏直接就是以自定義的ArrayAdapter做爲例子。
咱們這裏須要將學生的信息羅列出來,須要三個TextView:ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
在自定義ArrayAdapter的時候,最神奇的地方就是咱們能夠指定ArrayAdapter綁定的數據類型,能夠是基本數據類型,也能夠是自定義的對象類型,像是此次的User類型。對於自定義的ArrayAdapter的構造方法,存在不少形式,此次是傳進一個View的資源Id,可是咱們也能夠指定綁定的數據類型。
ArrayAdapter的神奇之處就是咱們居然能夠像是操做Array同樣來操做ArrayAdapter!像是例子中的添加操做,而其餘的適配器都是須要傳進一個容器的。ArrayAdapter爲何能夠處理對象類型的數據呢?其實,ArrayAdapter是使用數組中對象的toString()方法來填充指定的TextView,因此咱們能夠經過重寫對象的toString()方法來自定義ListView的顯示。佈局
@Override public View getView(int position, View convertView, ViewGroup parent) { User user = getItem(position); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(mResourceId, null); TextView text = (TextView) view.findViewById(R.id.info); text.setText(user.toString()); return view; } class User { private int mAge; private String mName; private String mSex; public User(int age, String name, String sex) { this.mAge = age; this.mName = name; this.mSex = sex; } @Override public String toString() { return "姓名:" + mName + " " + "年齡:" + mAge + " " + "性別:" + mSex; } }
這樣咱們能夠只在一行中顯示全部數據。this
使用ArrayAdapter最大的疑問就是咱們是否須要將一個現成的容器傳入ArrayAdapter中?本來ArrayAdapter自己就用通常容器的基本操做,像是添加新的元素等,但它自己並不能完成當成容器使用,咱們更多的時候是要將一個容器中的元素交給ArrayAdapter,由後者決定它的顯示形式。spa
class UserAdapter extends ArrayAdapter<User> { private int mResourceId; public UserAdapter(Context context, int textViewResourceId, List<User> users) { super(context, textViewResourceId, users); this.mResourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { User user = getItem(position); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(mResourceId, null); TextView text = (TextView) view.findViewById(R.id.info); text.setText(user.toString()); return view; } }
List<User> users = new ArrayList<User>(); users.add(new User(10, "小智", "男")); users.add(new User(10, "小霞", "女")); UserAdapter adapter = new UserAdapter(this, R.layout.list_item, users); listView.setAdapter(adapter);
若是咱們將ArrayAdapter綁定的數據類型定義爲Object,咱們能夠自由的傳入任何類型的容器而不須要任何有關類型轉換的操做!code
ArrayAdapter不單單是能夠顯示TextView,它當讓也像是其餘Adapter同樣,能夠顯示任何其餘非TextView的組件:orm
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) this.findViewById(R.id.list); List<Object> users = new ArrayList<Object>(); users.add(10); users.add(11); UserAdapter adapter = new UserAdapter(this, R.layout.list_item, R.id.info, users); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class UserAdapter extends ArrayAdapter<Object> { private int mResourceId; public UserAdapter(Context context, int resourceId, int textViewResourceId, List<Object> users) { super(context, resourceId, textViewResourceId, users); this.mResourceId = resourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { Object user = getItem(position); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(mResourceId, null); TextView text = (TextView) view.findViewById(R.id.info); text.setText(user.toString()); return view; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
若是咱們的佈局中須要其餘組件,必須指定該佈局中用於顯示ArrayAdapter中數據的TextView的Id。xml
若是隻是方便綁定數據的話,實際上是沒有必要專門獨立個ArrayAdapter出來,只要覆寫getView()就能夠,正如使用容器就是爲了方便大量數據的處理同樣的道理,使用ArrayAdapter也是爲了處理數據較大的狀況,像是超過100條或者頻繁動態增刪數據時,就可使用ArrayAdapter,並且,爲了方便咱們刷新UI,ArrayAdapter也提供了setNotifyOnChange()方法,這樣能夠下降UI的處理量,使得刷新UI更加快速,主要是經過中止對add,insert,remove和clear的操做來實現這點。