Android入門教程(十八)之–ListView (二) )

來源:互聯網
上載者:User

本文來自http://blog.csdn.net/hellogv/

 

ListView是一個經常用到的控制項,ListView裡面的每個子項Item可以使一個字串,也可以是一個群組控制項。先說說ListView的實現:

1.準備ListView要顯示的資料

2.使用 一維或多維 動態數組 儲存資料;

2.構建適配器 , 簡單地來說, 適配器就是 Item數組 動態數組 有多少元素就產生多少個Item;

3.把 適配器 添加到ListView,並顯示出來。


接下來,看看本文代碼所實現的ListView:

 

接下來,就開始UI的XML代碼:

main.xml代碼如下,很簡單,也不需要多做解釋了:

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.         android:id="@+id/LinearLayout01"   
  4.         android:layout_width="fill_parent"   
  5.         android:layout_height="fill_parent"   
  6.         xmlns:android="http://schemas.android.com/apk/res/android">  
  7.           
  8.         <ListView android:layout_width="wrap_content"   
  9.                   android:layout_height="wrap_content"   
  10.                   android:id="@+id/MyListView">  
  11.         </ListView>  
  12. </LinearLayout>  

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> android:id="@+id/LinearLayout01"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> xmlns:android="http://schemas.android.com/apk/res/android"></p><p> <ListView android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:id="@+id/MyListView"><br /> </ListView><br /></LinearLayout><br />

 

 

my_listitem.xml的代碼如下,my_listitem.xml用於設計ListView的Item:

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.         android:layout_width="fill_parent"   
  4.         xmlns:android="http://schemas.android.com/apk/res/android"   
  5.         android:orientation="vertical"  
  6.         android:layout_height="wrap_content"   
  7.         android:id="@+id/MyListItem"   
  8.         android:paddingBottom="3dip"   
  9.         android:paddingLeft="10dip">  
  10.         <TextView   
  11.                 android:layout_height="wrap_content"   
  12.                 android:layout_width="fill_parent"   
  13.                 android:id="@+id/ItemTitle"   
  14.                 android:textSize="30dip">  
  15.         </TextView>  
  16.         <TextView   
  17.                 android:layout_height="wrap_content"   
  18.                 android:layout_width="fill_parent"   
  19.                 android:id="@+id/ItemText">  
  20.         </TextView>  
  21. </LinearLayout>  

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> android:layout_width="fill_parent"<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_height="wrap_content"<br /> android:id="@+id/MyListItem"<br /> android:paddingBottom="3dip"<br /> android:paddingLeft="10dip"><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:layout_width="fill_parent"<br /> android:id="@+id/ItemTitle"<br /> android:textSize="30dip"><br /> </TextView><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:layout_width="fill_parent"<br /> android:id="@+id/ItemText"><br /> </TextView><br /></LinearLayout><br />

 

解釋一下,裡面用到的一些屬性:

1.paddingBottom="3dip",Layout往底部留出3個像素的空白地區

2.paddingLeft="10dip",Layout往左邊留出10個像素的空白地區

3.textSize="30dip",TextView的字型為30個像素那麼大。

 

最後就是JAVA的原始碼:

view plain copy to clipboard print ?
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.     //綁定XML中的ListView,作為Item的容器   
  5.     ListView list = (ListView) findViewById(R.id.MyListView);  
  6.       
  7.     //產生動態數組,並且轉載資料   
  8.     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();  
  9.     for(int i=0;i<30;i++)  
  10.     {  
  11.         HashMap<String, String> map = new HashMap<String, String>();  
  12.         map.put("ItemTitle", "This is Title.....");  
  13.         map.put("ItemText", "This is text.....");  
  14.         mylist.add(map);  
  15.     }  
  16.     //產生適配器,數組===》ListItem   
  17.     SimpleAdapter mSchedule = new SimpleAdapter(this, //沒什麼解釋   
  18.                                                 mylist,//資料來源    
  19.                                                 R.layout.my_listitem,//ListItem的XML實現   
  20.                                                   
  21.                                                 //動態數組與ListItem對應的子項           
  22.                                                 new String[] {"ItemTitle", "ItemText"},   
  23.                                                   
  24.                                                 //ListItem的XML檔案裡面的兩個TextView ID   
  25.                                                 new int[] {R.id.ItemTitle,R.id.ItemText});  
  26.     //添加並且顯示   
  27.     list.setAdapter(mSchedule);  
  28. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.