AdapterView的內容一般是包含多項相同格式資源的列表,常用的有5種AdapterView的子類:
(1)ListView:簡單的列表
(2)Spinner:下拉列表,給用戶提供選擇
(3)Gallery:縮略圖,已經被水平的ScrollView和ViewPicker取代,但也還算常用,是一個可以把子項以中心鎖定,水平滾動的列表
(4)GridView:網格圖,以表格形式顯示資源,可以左右滑動的
這里先介紹最簡單的顯示列表視圖ListView。
列表的顯示需要三個元素:
1.ListVeiw (或另外三個):用來展示列表的View。
2.Adapter適配器 :用來把數據映射到ListView上的中介,列表項的數據由Adapter提供
3.數據 :具體的將被映射的字符串,圖片,或者基本組件。
根據列表的適配器類型,列表分為4種:
(1)ArrayAdapter:它只能處理列表項內容全是文本的情況。
◆數據源:數組或者List<String>對象或者其他
(2)SimpleAdapter: 它不僅可以處理列表項全是文本的情況,當列表項中還有其他控件時,同樣可以處理,每個列表項可以定制更復雜的布局,組件等
◆數據源:只能為List<Map<“鍵”,“值”>>形式的數據
(3)自定義Adapter:繼承BaseAdapter,根據xml文件中定義的樣式驚醒列表項的填充,適用性最強。
(4)SimpleCursorAdapter:專門用于把游標Cursor(數據庫查詢集)中的數據映像到列表中(我們以后再來研究),用在SQLite數據庫較多
LisView組件常用的Xml屬性:

android:divider:設置List列表項的分隔條,即可用顏色區分,也可用Drawable對象分隔
android:dividerHeight:設置分隔條高度
**android:entries:指定一個數組資源,Android將根據該數據資源來生成ListView**
**一,使用android:entries屬性為LisView指定數組**
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="2dp"
android:entries="@array/books"
android:headerDividersEnabled="false" >
</ListView>
</LinearLayout>
~~~
arrays.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="books">
<item>北京</item>
<item>上海</item>
<item>廣州</item>
<item>深圳</item>
</string-array>
</resources>
~~~
上面一個ListView指定一個? android:entries="@array/books",使用了下面的數組資源
使用數組創建LisView十分簡單,但這種LisView能定制的內容很少,甚至連每個列表項的字號,顏色都不能改變

**二,使用ArrayAdapter創建LisView**
其中以ArrayAdapter最為簡單,**只能展示一行字**
ArrayAdapter的三個參數
* Context:代表訪問整個Android應用的接口。幾乎創建所有的組件都需要傳入Context對象
* textViewResourcedId:一個資源Id,該Id代表一個TextView,該TextView組件將作為AarryAdapter的列表組件,**這里的布局文件是指每個列表項的布局文件,可以自己定制,也可以使用android提供的布局文件**
* **數組或List**:負責為多個列表框提供數據
main.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 使用紅線分隔 -->
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="335dp"
android:divider="#f00"
android:dividerHeight="2dp"
android:headerDividersEnabled="false" >
</ListView>
<!-- 使用綠線分隔 -->
<ListView
android:id="@+id/listView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#0f0"
android:dividerHeight="2dp"
android:headerDividersEnabled="false" >
</ListView>
</LinearLayout>
~~~
array_item.xml 可以定制TextView的字體顏色了
~~~
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"
android:textSize="20dp" />
~~~
checked_item.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:checkMark="@drawable/ok"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>
~~~
MainActivity.java
~~~
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list1=(ListView) findViewById(R.id.listView1);
ListView list2=(ListView) findViewById(R.id.listView2);
String[] str1={"唐僧","孫悟空","豬八戒","沙和尚"};
//用數組包裝ArrayAdapter
/*ArrayAdapter的三個參數
* Context:代表訪問整個Android應用的接口。幾乎創建所有的組件都需要傳入Context對象
* textViewResourcedId:一個資源Id,該Id代表一個TextView,該TextView組件將作為AarryAdapter的列表組件
*
* 數組或List:負責為多個列表框提供數據*/
ArrayAdapter<String> ad1=new ArrayAdapter<String>(this,R.layout.array_item,str1);
//為ListView設置adapter
list1.setAdapter(ad1);
String[] str2={"湖北省","湖南省","江蘇省","浙江省"};
//用數組包裝ArrayAdapter
ArrayAdapter<String> ad2=new ArrayAdapter<String>(this,R.layout.checked_item,str2);
//為ListView設置adapter
list2.setAdapter(ad2);
}
~~~

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數據,要裝配這些數據就需要一個連接ListView視圖對象和數組數據的適配器來兩者的適配工作,同時用setAdapter()完成適配的最后工作
**三,使用SimpleAdapter創建ListView**
simpleAdapter的擴展性最好,**每個列表項可以定義各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(復選框)**等等。
simpleAdapter的數據源:只能為List<Map<“鍵”,“值”>>形式的數據
SimpleAdapter對象,需要5個參數,后面4個是關鍵
? ? ? ? ?* 第2個參數:是一個List<Map<? extends Map<string,?>>的集合對象,集合中的每個 Map<string,?>對象是一個列表項
? ? ? ? ?* 第3個參數:該參數指定一個列表項布局界面的ID。
? ? ? ? ?* 第4個參數:一個String[]類型的參數,決定提取Map對象中的那些key值對應的value類生成類表項?就是:**需要顯示value的key值**
? ? ? ? ?* 第5個參數:int[]類型的參數,決定填充哪些 組件,**就是使用顯示值得組件Id**
? ? ? ? ?* 注意:第4,5個參數的數組需要一一對應才行
使用SimpleAdapter提供列表項
main.xml
~~~
<?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" >
<!-- 定義一個ListView -->
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="255dp" >
</ListView>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:textColor="#f0f"
android:textSize="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:textColor="#f00"
android:textSize="20dp" />
</LinearLayout>
~~~
simple_item.xml,這是每個列表項的布局文件,左顯示一張圖片,右邊是上下分布的兩個文本框描述,這樣就更加復雜了ListView的列表項
~~~
<?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="horizontal" >
<!--定義一個ImageView,,作為列表項的一部分,id與java代碼中相同-->
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tiger" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--定義一個TextView,,作為列表項的一部分-->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#f0f"
android:textSize="18dp" />
<!--定義一個TextView,,作為列表項的一部分-->
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
~~~
MainActivity.java
~~~
public class MainActivity extends Activity {
private String[] names=new String[]{
"虎頭","弄玉","李清照","李白"
};
private String[] descs=new String[]{
"可愛的老虎","一個擅長音樂的女孩","一個擅長文學的女性","浪漫主義詩人"
};
private int[] imagesIds=new int[]{
R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView show=(TextView) findViewById(R.id.textView1);
final TextView show2=(TextView) findViewById(R.id.textView2);
//創建一個List集合。List集合的元素是Map
List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
//第一步:創建List集合
//for循環,為list賦值
for(int i=0;i<names.length;i++){
Map<String,Object> item=new HashMap<String,Object>();
item.put("header", imagesIds[i]);
item.put("personName", names[i]);
item.put("desc", descs[i]);
//把列表項放入到list中
listItems.add(item);
}
//第二步:設置SimpleAdapter的參數
/*SimpleAdapter對象,需要5個參數,后面4個是關鍵
* 第2個參數:是一個List<Map<? extends Map<string,?>>的集合對象,集合中的每個 Map<string,?>對象是一個列表項
* 第3個參數:該參數指定一個列表項布局界面的ID
* 第4個參數:一個String[]類型的參數,決定提取Map對象中的那些key值對應的value類生成類表項
* 就是:需要顯示值
* 第5個參數:int[]類型的參數,決定填充哪些 組件,就是使用顯示值得組件Id
* 注意:第4,5個參數的數組需要一一對應才行
*/
SimpleAdapter sd=new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"personName","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});
//為ListView設置Adapter
ListView list=(ListView) findViewById(R.id.listView1);
list.setAdapter(sd);//第三步,添加adapter
//為列表項設置單擊事件
list.setOnItemClickListener(new OnItemClickListener(){
//第position項被單擊時觸發該方法
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
show.setText(names[position]+"是"+descs[position]);
}
});
}
~~~

當需要監聽用戶單擊,選中某個列表項時間,可以通過adapterview的
setOnItemClickListener方法
~~~
//為列表項設置單擊事件
listview.setOnItemClickListener(new OnItemClickListener(){
//第position項被單擊時觸發該方法
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//添加操作
}
});
~~~
**使用SimpleAdapter分三步:**
1,創建List<Map<String,Object>>集合
2,設置SimpleAdapter參數
3,為ListView添加simpleadpater
四,擴展BaseAdapter創建ListView
**擴展BaseAdapter:**
(1)創建類,繼承自BaseAdapter
(2)重寫其中的四個方法
①int getCount():返回的是數據源對象的個數,即列表項數
②Object getItem(int position):返回指定位置position上的列表
③long getItemId(int position):返回指定位置處的行ID
④View getView():返回列表項對應的視圖,方法體中
◆實例化視圖填充器
◆用視圖填充器,根據Xml文件,實例化視圖
◆根據布局找到控件,并設置屬性
◆返回View視圖
main.xml
~~~
<?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">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
~~~
MainActivity.java
~~~
package com.hust.baseadaptertest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView mylistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylistview=(ListView)findViewById(R.id.listView1);
//自定義baseadapter:程序要創建多少個列表項,每個列表項的組件都有開發者來決定
BaseAdapter ba=new BaseAdapter(){
//自動重寫這4個函數
@Override
public int getCount() {
// TODO Auto-generated method stub
return 20;//指定一共包含20個選項
}
//返回值決定第position處的列表項內容
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
//重寫該方法,該方法的返回值將作為列表項的ID
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//重寫該方法,該方法返回的View將作為列表框,決定第position處的列表項組件對象
/*
* Get a View that displays the data at the specified position in the data set.
* You can either create a View manually or inflate it from an XML layout file.
* When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters
* unless you use android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)
* to specify a root view and to prevent attachment to the root.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/*
* 手動創建列表項的布局文件
* */
//創建以個LinearLayout,并向其中添加兩個組件
LinearLayout line=new LinearLayout(MainActivity.this);
line.setOrientation(0);
//一個ImageView組件
ImageView image=new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
//一個TextView組件
TextView text=new TextView(MainActivity.this);
text.setText("第"+(position+1)+"個列表項");
text.setTextSize(20);
text.setTextColor(Color.RED);
//兩個組件添加到布局組件中
line.addView(image);
line.addView(text);
//返回布局組件
return line;
}
};
//設置adapter
mylistview.setAdapter(ba);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~

總結一下:
1:聲明AdapterView對象子類(ListView,Spinner,Gallary,GridView),根據ID利用findViewById方法找到此對象
2:聲明Adapter對象,根據構造方法實例化此對象。具體如下:
(1)ArrayAdapter<數據類型> adapter = new ArrayAdapter<數據類型>(context:一般指當前Activity對象,layout:每個列表項顯示的布局,data:數據源變量);
(2)SimpleAdapter adapter = new SimpleAdapter(context:一般指當前Activity對象,data:數據源變量,layout:每個列表項顯示的布局,new String[]{}:數據源中的“鍵”,new int[]{}:顯示數據源的控件ID);
(3)自定義Adapter類 adapter = new 自定義Adapter類構造方法;
3:綁定Adapter對象到Adapter上
AdapterView對象.setAdapter(Adapter對象);
- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite