• <kbd id="qyk40"></kbd>
  • <strike id="qyk40"></strike><samp id="qyk40"><pre id="qyk40"></pre></samp>

    在android開發(fā)中ListView是比較常用的組件,它以列表的形式展示具體內(nèi)容,并且能夠根據(jù)數(shù)據(jù)的長(zhǎng)度自適應(yīng)顯示。抽空把對(duì)ListView的使用做了整理,并寫了個(gè)小例子,如下圖。

     列表的顯示需要三個(gè)元素:

    1.ListVeiw 用來展示列表的View。

    2.適配器 用來把數(shù)據(jù)映射到ListView上的中介。

    3.?dāng)?shù)據(jù)    具體的將被映射的字符串,圖片,或者基本組件。

    根據(jù)列表的適配器類型,列表分為三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

    其 中以ArrayAdapter最為簡(jiǎn)單,只能展示一行字。SimpleAdapter有最好的擴(kuò)充性,可以自定義出各種效果。 SimpleCursorAdapter可以認(rèn)為是SimpleAdapter對(duì)數(shù)據(jù)庫的簡(jiǎn)單結(jié)合,可以方面的把數(shù)據(jù)庫的內(nèi)容以列表的形式展示出來。

     我們從最簡(jiǎn)單的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
    /**
     * @author allin
     *
     */
    public class MyListView extends Activity {
     
        private ListView listView;
        //private List<String> data = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            
            listView = new ListView(this);
            listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
            setContentView(listView);
        }
        
        
        
        private List<String> getData(){
            
            List<String> data = new ArrayList<String>();
            data.add("測(cè)試數(shù)據(jù)1");
            data.add("測(cè)試數(shù)據(jù)2");
            data.add("測(cè)試數(shù)據(jù)3");
            data.add("測(cè)試數(shù)據(jù)4");
            
            return data;
        }
    }

    上面代碼使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數(shù)據(jù),要裝配這些數(shù)據(jù)就需要一個(gè)連接ListView視圖對(duì)象和數(shù)組數(shù)據(jù)的適配器來兩者的適配工作,ArrayAdapter的 構(gòu)造需要三個(gè)參數(shù),依次為this,布局文件(注意這里的布局文件描述的是列表的每一行的布 局,android.R.layout.simple_list_item_1是系統(tǒng)定義好的布局文件只顯示一行文字,數(shù)據(jù)源(一個(gè)List集合)。同時(shí) 用setAdapter()完成適配的最后工作。運(yùn)行后的現(xiàn)實(shí)結(jié)構(gòu)如下圖:

    SimpleCursorAdapter

       sdk的解釋是這樣的:An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。簡(jiǎn)單的說就是方便把從游標(biāo)得到的數(shù)據(jù)進(jìn)行列表顯示,并可以把指定的列映射到對(duì)應(yīng)的TextView中。

      下面的程序是從電話簿中把聯(lián)系人顯示到類表中。先在通訊錄中添加一個(gè)聯(lián)系人作為數(shù)據(jù)庫的數(shù)據(jù)。然后獲得一個(gè)指向數(shù)據(jù)庫的Cursor并且定義一個(gè)布局文件(當(dāng)然也可以使用系統(tǒng)自帶的)。

    ?

    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
    /**
     * @author allin
     *
     */
    public class MyListView2 extends Activity {
     
        private ListView listView;
        //private List<String> data = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            
            listView = new ListView(this);
            
            Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
            startManagingCursor(cursor);
            
            ListAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1,
                    cursor,
                    new String[]{People.NAME},
                    new int[]{android.R.id.text1});
            
            listView.setAdapter(listAdapter);
            setContentView(listView);
        }
        
        
    }

     Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先獲得一個(gè)指向系統(tǒng)通訊錄數(shù)據(jù)庫的Cursor對(duì)象獲得數(shù)據(jù)來源。

     startManagingCursor(cursor);我們將獲得的Cursor對(duì)象交由Activity管理,這樣Cursor的生命周期和Activity便能夠自動(dòng)同步,省去自己手動(dòng)管理Cursor。

     SimpleCursorAdapter 構(gòu)造函數(shù)前面3個(gè)參數(shù)和ArrayAdapter是一樣的,最后兩個(gè)參數(shù):一個(gè)包含數(shù)據(jù)庫的列的String型數(shù)組,一個(gè)包含布局文件中對(duì)應(yīng)組件id的 int型數(shù)組。其作用是自動(dòng)的將String型數(shù)組所表示的每一列數(shù)據(jù)映射到布局文件對(duì)應(yīng)id的組件上。上面的代碼,將NAME列的數(shù)據(jù)一次映射到布局文 件的id為text1的組件上。

    注意:需要在AndroidManifest.xml中如權(quán)限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

    運(yùn)行后效果如下圖:

    SimpleAdapter

    simpleAdapter 的擴(kuò)展性最好,可以定義各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(復(fù)選框)等等。下 面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差別,不同就是對(duì)顯示ListView做了 許多優(yōu)化,方面顯示而已。

    下面的程序是實(shí)現(xiàn)一個(gè)帶有圖片的類表。

    首先需要定義好一個(gè)用來顯示每一個(gè)列內(nèi)容的xml

    vlist.xml

    ?

    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
     
     
        <ImageView android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"/>
     
        <LinearLayout android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
     
            <TextView android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF"
                android:textSize="22px" />
            <TextView android:id="@+id/info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF"
                android:textSize="13px" />
     
        </LinearLayout>
     
     
    </LinearLayout>

    下面是實(shí)現(xiàn)代碼:

    ?

    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
    42
    /**
     * @author allin
     *
     */
    public class MyListView3 extends ListActivity {
     
     
        // private List<String> data = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
            SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
                    new String[]{"title","info","img"},
                    new int[]{R.id.title,R.id.info,R.id.img});
            setListAdapter(adapter);
        }
     
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
     
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
     
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
     
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }
    }

    使 用simpleAdapter的數(shù)據(jù)用一般都是HashMap構(gòu)成的List,list的每一節(jié)對(duì)應(yīng)ListView的每一行。HashMap的每個(gè)鍵值 數(shù)據(jù)映射到布局文件中對(duì)應(yīng)id的組件上。因?yàn)橄到y(tǒng)沒有對(duì)應(yīng)的布局文件可用,我們可以自己定義一個(gè)布局vlist.xml。下面做適配,new一個(gè) SimpleAdapter參數(shù)一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的組件id,title,info,img。布局文件的各組件分別映射到HashMap的各元素上,完成適配。

    運(yùn)行效果如下圖:

    有按鈕的ListView

    但 是有時(shí)候,列表不光會(huì)用來做顯示用,我們同樣可以在在上面添加按鈕。添加按鈕首先要寫一個(gè)有按鈕的xml文件,然后自然會(huì)想到用上面的方法定義一個(gè)適配 器,然后將數(shù)據(jù)映射到布局文件上。但是事實(shí)并非這樣,因?yàn)榘粹o是無法映射的,即使你成功的用布局文件顯示出了按鈕也無法添加按鈕的響應(yīng),這時(shí)就要研究一下 ListView是如何現(xiàn)實(shí)的了,而且必須要重寫一個(gè)類繼承BaseAdapter。下面的示例將顯示一個(gè)按鈕和一個(gè)圖片,兩行字如果單擊按鈕將刪除此按 鈕的所在行。并告訴你ListView究竟是如何工作的。效果如下:

    vlist2.xml

    ?

    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
     
     
        <ImageView android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"/>
     
        <LinearLayout android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
     
            <TextView android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF"
                android:textSize="22px" />
            <TextView android:id="@+id/info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF"
                android:textSize="13px" />
     
        </LinearLayout>
     
     
        <Button android:id="@+id/view_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/s_view_btn"
            android:layout_gravity="bottom|right" />
    </LinearLayout>

    程序代碼:

    ?

    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    /**
     * @author allin
     *
     */
    public class MyListView4 extends ListActivity {
     
     
        private List<Map<String, Object>> mData;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mData = getData();
            MyAdapter adapter = new MyAdapter(this);
            setListAdapter(adapter);
        }
     
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
     
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
     
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
     
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }
        
        // ListView 中某項(xiàng)被選中后的邏輯
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            
            Log.v("MyListView4-click", (String)mData.get(position).get("title"));
        }
        
        /**
         * listview中點(diǎn)擊按鍵彈出對(duì)話框
         */
        public void showInfo(){
            new AlertDialog.Builder(this)
            .setTitle("我的listview")
            .setMessage("介紹...")
            .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            })
            .show();
            
        }
        
        
        
        public final class ViewHolder{
            public ImageView img;
            public TextView title;
            public TextView info;
            public Button viewBtn;
        }
        
        
        public class MyAdapter extends BaseAdapter{
     
            private LayoutInflater mInflater;
            
            
            public MyAdapter(Context context){
                this.mInflater = LayoutInflater.from(context);
            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mData.size();
            }
     
            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }
     
            @Override
            public long getItemId(int arg0) {
                // TODO Auto-generated method stub
                return 0;
            }
     
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                
                ViewHolder holder = null;
                if (convertView == null) {
                    
                    holder=new ViewHolder(); 
                    
                    convertView = mInflater.inflate(R.layout.vlist2, null);
                    holder.img = (ImageView)convertView.findViewById(R.id.img);
                    holder.title = (TextView)convertView.findViewById(R.id.title);
                    holder.info = (TextView)convertView.findViewById(R.id.info);
                    holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
                    convertView.setTag(holder);
                    
                }else {
                    
                    holder = (ViewHolder)convertView.getTag();
                }
                
                
                holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
                holder.title.setText((String)mData.get(position).get("title"));
                holder.info.setText((String)mData.get(position).get("info"));
                
                holder.viewBtn.setOnClickListener(new View.OnClickListener() {
                    
                    @Override
                    public void onClick(View v) {
                        showInfo();                
                    }
                });
                
                
                return convertView;
            }
            
        }
        
        
        
        
    }

       下面將對(duì)上述代碼,做詳細(xì)的解釋,listView在開始繪制的時(shí)候,系統(tǒng)首先調(diào)用getCount()函數(shù),根據(jù)他的返回值得到listView的長(zhǎng) 度(這也是為什么在開始的第一張圖特別的標(biāo)出列表長(zhǎng)度),然后根據(jù)這個(gè)長(zhǎng)度,調(diào)用getView()逐一繪制每一行。如果你的getCount()返回值 是0的話,列表將不顯示同樣return 1,就只顯示一行。

      系統(tǒng)顯示列表時(shí),首先實(shí)例化一個(gè)適配器(這里將實(shí)例化自定義的適配器)。 當(dāng)手動(dòng)完成適配時(shí),必須手動(dòng)映射數(shù)據(jù),這需要重寫getView()方法。系統(tǒng)在繪制列表的每一行的時(shí)候?qū)⒄{(diào)用此方法。getView()有三個(gè)參 數(shù),position表示將顯示的是第幾行,covertView是從布局文件中inflate來的布局。我們用LayoutInflater的方法將定 義好的vlist2.xml文件提取成View實(shí)例用來顯示。然后將xml文件中的各個(gè)組件實(shí)例化(簡(jiǎn)單的findViewById()方法)。這樣便可 以將數(shù)據(jù)對(duì)應(yīng)到各個(gè)組件上了。但是按鈕為了響應(yīng)點(diǎn)擊事件,需要為它添加點(diǎn)擊監(jiān)聽器,這樣就能捕獲點(diǎn)擊事件。至此一個(gè)自定義的listView就完成了,現(xiàn) 在讓我們回過頭從新審視這個(gè)過程。系統(tǒng)要繪制ListView了,他首先獲得要繪制的這個(gè)列表的長(zhǎng)度,然后開始繪制第一行,怎么繪制呢?調(diào)用 getView()函數(shù)。在這個(gè)函數(shù)里面首先獲得一個(gè)View(實(shí)際上是一個(gè)ViewGroup),然后再實(shí)例并設(shè)置各個(gè)組件,顯示之。好了,繪制完這一 行了。那 再繪制下一行,直到繪完為止。在實(shí)際的運(yùn)行過程中會(huì)發(fā)現(xiàn)listView的每一行沒有焦點(diǎn)了,這是因?yàn)锽utton搶奪了listView的焦點(diǎn),只要布 局文件中將Button設(shè)置為沒有焦點(diǎn)就OK了。

    運(yùn)行效果如下圖:

     

    穩(wěn)定

    產(chǎn)品高可用性高并發(fā)

    貼心

    項(xiàng)目群及時(shí)溝通

    專業(yè)

    產(chǎn)品經(jīng)理1v1支持

    快速

    MVP模式小步快跑

    承諾

    我們選擇聲譽(yù)

    堅(jiān)持

    10年專注高端品質(zhì)開發(fā)
    • 返回頂部
    久久精品国产四虎| 国产成人精品2021| 日韩少妇无码喷潮系列一二三| 国产精品一二二区| 99re视频精品全部免费| 亚洲国产成人一区二区精品区| 国产在线精品一区二区三区直播 | 人人鲁人人莫人人爱精品| 亚洲国产成人乱码精品女人久久久不卡| 免费久久精品国产片香蕉| 揄拍成人国产精品视频| 国产精品99久久99久久久动漫| 日日夜夜精品视频| 国产精品亚洲综合五月天| 国产成人精品视频在放| 久久精品免费一区二区| 亚洲熟妇无码久久精品| 5x社区精品视频在线播放18| 久久亚洲精品中文字幕| 色欲精品国产一区二区三区AV| 久久久国产乱子伦精品| 国产精品成熟老女人视频| 日本精品久久久中文字幕| 久久99热国产这有精品| 日本精品在线视频| 亚洲国产精品乱码一区二区| 国产成人精品一区二区三区免费| 国产亚洲精品一品区99热| 亚洲精品国产成人片| 国产乱码精品一品二品| 老司机99精品99| 久久国产精品亚洲综合| 久9久9精品免费观看| 99精品久久99久久久久久| 99这里只精品热在线获取| 91精品久久久久久久久久小网站 | 麻豆一区二区三区精品视频| 久久亚洲AV无码精品色午夜麻豆| 亚洲国产精品xo在线观看| AAA级久久久精品无码片| 亚洲日韩一区精品射精|