<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    風人園

    弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
    隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
    數據加載中……

    android開發教程之listview使用方法

    首先是布局文件,這里需要兩個布局文件,一個是放置列表控件的Activity對應的布局文件 main.xml,另一個是ListView中每一行信息顯示所對應的布局 list_item.xml 這一步需要注意的問題是ListView 控件的id要使用Android系統內置的 android:id="@android:id/list"

    main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width
    ="match_parent"
        android:layout_height
    ="match_parent"
        android:orientation
    ="vertical" >
            
    <ListView 
            
    android:id="@android:id/list"
            android:layout_width
    ="match_parent"
            android:layout_height
    ="match_parent"
            android:padding
    ="20dip"/>
    </LinearLayout>

    list_item.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="match_parent"
    android:orientation
    ="horizontal" >

    <TextView
    android:id="@+id/user_name"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="match_parent"
    android:layout_weight
    ="1"/>
    <TextView
    android:id="@+id/user_id"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="match_parent"
    android:layout_weight
    ="1"/>
    </LinearLayout>


    然后就設置MainActivity中的代碼了:基本思想就是先將數據添加到ArrayList中,然后在設置SimpleAdapter適配器完成設置,入下:

    package com.example.android_newlistview;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.view.Menu;
    import android.widget.SimpleAdapter;

    public class MainActivity extends ListActivity {


    String[] from
    ={"name","id"}; //這里是ListView顯示內容每一列的列名
    int[] to={R.id.user_name,R.id.user_id}; //這里是ListView顯示每一列對應的list_item中控件的id

    String[] userName
    ={"zhangsan","lisi","wangwu","zhaoliu"}; //這里第一列所要顯示的人名
    String[] userId={"1001","1002","1003","1004"}; //這里是人名對應的ID

    ArrayList
    <HashMap<String,String>> list=null;
    HashMap
    <String,String> map=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //為MainActivity設置主布局
    //創建ArrayList對象;
    list=new ArrayList<HashMap<String,String>>();
    //將數據存放進ArrayList對象中,數據安排的結構是,ListView的一行數據對應一個HashMap對象,
    //HashMap對象,以列名作為鍵,以該列的值作為Value,將各列信息添加進map中,然后再把每一列對應
    //的map對象添加到ArrayList中

    for(int i=0; i<4; i++){
    map
    =new HashMap<String,String>(); //為避免產生空指針異常,有幾列就創建幾個map對象
    map.put("id", userId[i]);
    map.put(
    "name", userName[i]);
    list.add(map);
    }


    //創建一個SimpleAdapter對象
    SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.list_item,from,to);
    //調用ListActivity的setListAdapter方法,為ListView設置適配器
    setListAdapter(adapter);
    }

    }


    另外對點擊某一行作出響應的方法是覆寫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);
    }


    posted on 2016-12-01 13:08 風人園 閱讀(168) 評論(0)  編輯  收藏 所屬分類: Android

    主站蜘蛛池模板: 337P日本欧洲亚洲大胆艺术图| 亚洲AV无码乱码国产麻豆| 亚洲AV综合色区无码二区偷拍 | 亚洲色大成网站www永久一区| 无遮挡呻吟娇喘视频免费播放| 国产在线播放免费| 国产精品亚洲专区在线播放| 国产一区二区三区免费看| 亚洲AV一区二区三区四区| 国产一级淫片免费播放电影| 国产成人va亚洲电影| 亚洲国产精品成人网址天堂| 一级看片免费视频囗交| 亚洲欧洲美洲无码精品VA| 国产一区二区免费视频| 亚洲精品第五页中文字幕| 国产乱码免费卡1卡二卡3卡| 亚洲人成无码网站在线观看| 国产免费变态视频网址网站| 国产精品福利在线观看免费不卡| 久久亚洲国产中v天仙www| 69xx免费观看视频| 亚洲大成色www永久网址| 免费人成在线观看网站视频| 羞羞视频免费网站在线看| 亚洲成年人在线观看| 国产桃色在线成免费视频| 国产亚洲精品91| 亚洲动漫精品无码av天堂| 免费无码A片一区二三区 | 国产一级a毛一级a看免费人娇| 亚洲AV无码乱码国产麻豆| 天天摸天天操免费播放小视频 | 亚洲国产精品久久久久久| 国产免费看JIZZ视频| 特级毛片aaaa免费观看| 亚洲视频一区在线播放| 成人国产mv免费视频| 国产亚洲免费的视频看| 国产精品亚洲专区无码WEB| 亚洲国产成人片在线观看无码 |