91亚洲精华国内精华精华液_国产高清在线精品一区不卡_精品特级一级毛片免费观看_欧美日韩中文制服有码_亚洲精品无码你懂的网站369

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

 列表的顯示需要三個元素:

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

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

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

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

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

 我們從最簡單的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("測試數(shù)據(jù)1");
        data.add("測試數(shù)據(jù)2");
        data.add("測試數(shù)據(jù)3");
        data.add("測試數(shù)據(jù)4");
        
        return data;
    }
}

上面代碼使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數(shù)據(jù),要裝配這些數(shù)據(jù)就需要一個連接ListView視圖對象和數(shù)組數(shù)據(jù)的適配器來兩者的適配工作,ArrayAdapter的 構(gòu)造需要三個參數(shù),依次為this,布局文件(注意這里的布局文件描述的是列表的每一行的布 局,android.R.layout.simple_list_item_1是系統(tǒng)定義好的布局文件只顯示一行文字,數(shù)據(jù)源(一個List集合)。同時 用setAdapter()完成適配的最后工作。運行后的現(xiàn)實結(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。簡單的說就是方便把從游標得到的數(shù)據(jù)進行列表顯示,并可以把指定的列映射到對應(yīng)的TextView中。

  下面的程序是從電話簿中把聯(lián)系人顯示到類表中。先在通訊錄中添加一個聯(lián)系人作為數(shù)據(jù)庫的數(shù)據(jù)。然后獲得一個指向數(shù)據(jù)庫的Cursor并且定義一個布局文件(當然也可以使用系統(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);先獲得一個指向系統(tǒng)通訊錄數(shù)據(jù)庫的Cursor對象獲得數(shù)據(jù)來源。

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

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

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

運行后效果如下圖:

SimpleAdapter

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

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

首先需要定義好一個用來顯示每一個列內(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>

下面是實現(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é)對應(yīng)ListView的每一行。HashMap的每個鍵值 數(shù)據(jù)映射到布局文件中對應(yīng)id的組件上。因為系統(tǒng)沒有對應(yīng)的布局文件可用,我們可以自己定義一個布局vlist.xml。下面做適配,new一個 SimpleAdapter參數(shù)一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的組件id,title,info,img。布局文件的各組件分別映射到HashMap的各元素上,完成適配。

運行效果如下圖:

有按鈕的ListView

但 是有時候,列表不光會用來做顯示用,我們同樣可以在在上面添加按鈕。添加按鈕首先要寫一個有按鈕的xml文件,然后自然會想到用上面的方法定義一個適配 器,然后將數(shù)據(jù)映射到布局文件上。但是事實并非這樣,因為按鈕是無法映射的,即使你成功的用布局文件顯示出了按鈕也無法添加按鈕的響應(yīng),這時就要研究一下 ListView是如何現(xiàn)實的了,而且必須要重寫一個類繼承BaseAdapter。下面的示例將顯示一個按鈕和一個圖片,兩行字如果單擊按鈕將刪除此按 鈕的所在行。并告訴你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 中某項被選中后的邏輯
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        
        Log.v("MyListView4-click", (String)mData.get(position).get("title"));
    }
    
    /**
     * listview中點擊按鍵彈出對話框
     */
    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;
        }
        
    }
    
    
    
    
}

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

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

運行效果如下圖:

 

穩(wěn)定

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

貼心

項目群及時溝通

專業(yè)

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

快速

MVP模式小步快跑

承諾

我們選擇聲譽

堅持

10年專注高端品質(zhì)開發(fā)
  • 返回頂部