教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Android培訓(xùn)之AdapterView與Adapter(2)

更新時間:2016年10月26日17時52分 來源:傳智播客Android培訓(xùn)學(xué)院 瀏覽次數(shù):

6:數(shù)據(jù)填充

借助下面的Adapter適配器對象從數(shù)據(jù)源中進(jìn)行適配

二:Adapter-->C(控制器)

1:作用

把數(shù)據(jù)源中數(shù)據(jù)以某種樣式(xml文件)顯示在視圖中。

2:分類

(1)ArrayAdapter:他只能處理列表項(xiàng)內(nèi)容全是文本的情況。

       ◆數(shù)據(jù)源:數(shù)組或者List<String>對象或者其他

(2)SimpleAdapter: 他不僅可以處理列表項(xiàng)全是文本的情況,當(dāng)列表項(xiàng)中還有其他控件時,同樣可以處理。

       ◆數(shù)據(jù)源:只能為List<Map<“鍵”,“值”>>形式的數(shù)據(jù)

(3)自定義Adapter:根據(jù)xml文件中定義的樣式驚醒列表項(xiàng)的填充,適用性最強(qiáng)。

(4)SimpleCursorAdapter:專門用于把游標(biāo)中的數(shù)據(jù)映像到列表中(我們以后再來研究)

3:自定義Adapter

1)創(chuàng)建類,繼承自BaseAdapter

(2)重寫其中的四個方法

①int getCount():返回的是數(shù)據(jù)源對象的個數(shù),即列表項(xiàng)數(shù)

②Object getItem(int position):返回指定位置position上的列表

③long getItemId(int position):返回指定位置處的行ID

View getView():返回列表項(xiàng)對應(yīng)的視圖,方法體中

實(shí)例化視圖填充器

◆用視圖填充器,根據(jù)Xml文件,實(shí)例化視圖

◆根據(jù)布局找到控件,并設(shè)置屬性

◆返回View視圖

三:數(shù)據(jù)填充

1:聲明AdapterView對象,根據(jù)ID利用findViewById方法找到此對象

2:聲明Adapter對象,根據(jù)構(gòu)造方法實(shí)例化此對象。具體如下:

(1)ArrayAdapter<數(shù)據(jù)類型> adapter = new ArrayAdapter<數(shù)據(jù)類型>(context:一般指當(dāng)前Activity對象,layout:每個列表項(xiàng)顯示的布局,data:數(shù)據(jù)源變量);

(2)SimpleAdapter adapter = new SimpleAdapter(context:一般指當(dāng)前Activity對象,data:數(shù)據(jù)源變量,layout:每個列表項(xiàng)顯示的布局,new String[]{}:數(shù)據(jù)源中的“鍵”,new int[]{}:顯示數(shù)據(jù)源的控件ID);

   (3)自定義Adapter類 adapter = new 自定義Adapter類構(gòu)造方法;

3:綁定Adapter對象到Adapter上

AdapterView對象.setAdapter(Adapter對象);

 

   四:具體應(yīng)用

1:用ArrayAdapter填充ListView  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class MainActivity extends Activity {
    private ListView lv;// 適配器控件------->V視圖
    private ArrayAdapter<String> adapter;// 適配器------>C控制器
    private String[] data = { "我是第1個列表項(xiàng)""我是第2個列表項(xiàng)""我是第3個列表項(xiàng)""我是第4個列表項(xiàng)",
            "我是第5個列表項(xiàng)""我是第6個列表項(xiàng)""我是第7個列表項(xiàng)" };// 數(shù)據(jù)源-->M
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到ListView
        lv = (ListView) findViewById(R.id.listView1);
        // 實(shí)現(xiàn)適配器,利用系統(tǒng)定義的樣式,加載數(shù)據(jù)源
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data);
        // R.layout.cell 自己定義視圖
        // android.R.layout.simple_list_item_1 系統(tǒng)定義視圖樣式
        // 綁定適配器到適配器控件上
        lv.setAdapter(adapter);
        //處理單擊事件:列表項(xiàng)被單擊時給出提示信息
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,
                        "第" + (position + 1) + "項(xiàng)被單擊按下", Toast.LENGTH_LONG)
                        .show();
            }
        });
        //處理長時間按下事件:列表項(xiàng)被長時間按下時給出提示信息
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,
                        "第" + (position + 1) + "項(xiàng)被長時間按下", Toast.LENGTH_LONG)
                        .show();
                return true;
            }
        });
    }
}


本文版權(quán)歸傳智播客Android培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明作者出處。謝謝!
作者:傳智播客Android培訓(xùn)學(xué)院
首發(fā):http://www.xamj520.com/Android
0 分享到:
和我們在線交談!