Android ListView控制項基本用法

來源:互聯網
上載者:User
  1. 下面的代碼是mars老師視頻教程裡的代碼,我把它注釋了一下。
  2. 建立兩個XML布局檔案main.xml和user.xml。main.xml檔案為系統自動建立
    main.xml布局檔案代碼:
    <?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent" ></p><p><LinearLayout android:id="@+id/listLinearLayout" <!-- 設定LinearLayout的ID --><br /> android:layout_width="fill_parent" <!-- 設定LinearLayout寬度為填滿整個螢幕 --><br /> android:layout_height="wrap_content" <!-- 設定LinearLayout高度適應內部控制項的高度 --><br /> android:orientation="vertical"> <!-- 設定LinearLayout的相片順序為縱向排列 --></p><p><!-- 在LinearLayout裡嵌套一個ListView控制項 --><br /><ListView android:id="@id/android:list" <!-- 設定ListView控制項的ID,這個ID為android系統內建ID --><br /> android:layout_width="fill_parent" <!-- 設定ListView控制項的寬度為填滿整個螢幕 --><br /> android:layout_height="wrap_content" <!-- 設定ListView控制項的高度為自適應 --><br /> android:drawSelectorOnTop="false" <!-- 設定ListView控制列目被按下時背景顏色在文字背後,設定成True時背景色會覆蓋文字 --><br /> android:scrollbars="vertical"/><!-- 設定ListView控制項捲軸的方向為縱向 --></p><p></LinearLayout><br /></LinearLayout>  
    ListViw控制項中的ID (android:id="@id/android:list") 是Android系統內建的ID,不能改為其他。
    android:drawSelectorOnTop="false" <!-- 當設定為false時條目被按下時背景顏色在文字背後,設定成True時背景色會覆蓋文字 
    user.xml布局檔案代碼:
    <?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"></p><p><TextView android:id="@+id/user_name"<br /> android:layout_width="180dip"<br /> android:layout_height="wrap_content"<br /> android:singleLine="true"<br /> android:textSize="10pt"<br /> android:paddingTop="2dip"<br /> android:paddingLeft="2dip"<br /> /></p><p><TextView android:id="@+id/user_ip"<br /> android:layout_width="180dip"<br /> android:layout_height="wrap_content"<br /> android:textSize="10pt"<br /> android:singleLine="true"<br /> android:paddingTop="2dip"<br /> android:paddingRight="2dip"<br /> /></p><p></LinearLayout><br />  
  3. 在Java原始碼檔案中寫入如下代碼:
    package paj.ListView;<br />import java.util.ArrayList;<br />import java.util.HashMap;<br />import android.app.Activity;<br />import android.app.ListActivity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.ListView;<br />import android.widget.SimpleAdapter;<br />public class ListViewMain extends ListActivity{<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> //產生一個ArrayList類型的變數list<br /> ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();<br /> //產生兩個HashMap類型的變數map1 , map2<br /> //HashMpa為索引值對類型。第一個參數為建,第二個參數為值<br /> HashMap<String, String> map1 = new HashMap<String, String>();<br /> HashMap<String, String> map2 = new HashMap<String, String>();<br /> //把資料填充到map1和map2中。<br /> map1.put("user_name", "張三");<br /> map1.put("user_ip", "192.168.1.52");</p><p> map2.put("user_name", "李四");<br /> map2.put("user_ip", "192.168.0.1");<br /> //把map1和map2添加到list中<br /> list.add(map1);<br /> list.add(map2);<br /> //產生一個SimpleAdapter類型的變數來填充資料<br /> SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name" , "user_ip"}, new int[]{R.id.user_name , R.id.user_ip});<br /> //設定顯示ListView<br /> setListAdapter(listAdapter);</p><p> }<br /> //重寫onListItemClick但是ListView條目事件<br />@Override<br />protected void onListItemClick(ListView l, View v, int position, long id) {<br />// TODO Auto-generated method stub<br />super.onListItemClick(l, v, position, id);<br />//顯示單擊條目ID號<br />System.out.println("id = " + id);<br />//顯示所單擊條目的位置數<br />System.out.println("position = " + position);<br />}</p><p>}   

    //產生SimpleAdapter對象參數的解釋
    SimpleAdapter listAdapter = new SimpleAdapter(<br /> this //this是當前Activity的對象<br /> , list//list為填充資料後的ArrayList類型的list對象<br /> , R.layout.user//這個就是之前建立的第二個布局檔案user.xml的id。<br /> , new String[]{"user_name" , "user_ip"}//這個String數組中的元素就是list對象中的列,list中有幾這個數組中就要寫幾列。<br /> //其中的元素必須是list中列的名。<br /> , new int[]{R.id.user_name , R.id.user_ip}//這個int型數組中的元素對應著上上一個參數String類型數組中的列名。<br /> //它的值是對應user.xml布局檔案中的TextView的id<br /> );   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.