Android開發系列(五) ListView的初步使用

來源:互聯網
上載者:User

  幾個月前接觸Android開發的時候曾經看過ListView的一些基礎的使用方法,現在又基本上忘記了,這裡總結一下,ListView分成兩部分來寫吧,首先講一下最基本的ListView的使用方法,下一節介紹一下通過繼承BaseAdapter來實現功能比較全面的ListView。其實我對ListView的使用還不是太透徹,若有大神忘不吝指教~

  

  

  首先是布局檔案,這裡需要兩個布局檔案,一個是放置清單控制項的Activity對應的布局檔案 main.xml,另一個是ListView中每一行資訊顯示所對應的布局  list_item.xml    這一步需要注意的問題是ListView 控制項的id要使用Android系統內建的 android:id="@android:id/list"   [注意形式]

 

main.xml

                 


list_item.xml

                 


 

然後就設定MainActivity中的代碼了:基本思想就是先將資料添加到ArrayList中,然後在設定SimpleAdapter適配器完成設定,入下:

  MainActivity ={"name","id"};                  [] to={R.id.user_name,R.id.user_id};   ={"zhangsan","lisi","wangwu","zhaoliu"};     String[] userId={"1001","1002","1003","1004"};  <HashMap<String,String>> list=<String,String> map=                 list= ArrayList<HashMap<String,String>>                ( i=0; i<4; i++= HashMap<String,String>();                   map.put("id""name"        SimpleAdapter adapter= SimpleAdapter(


另附圖兩張:

ListView資料結構:

結果圖:

 

另外對點擊某一行作出響應的方法是覆寫onListItemClick方法,根據返回的position(從0開始):

@Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);
  
  
 }

另外ListView進階教程已出,見連結http://www.cnblogs.com/carlos-vic/p/Carlos_V_Android_6.html

聯繫我們

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