1. 程式人生 > >ListView的使用小記(使用SimpleAdapter顯示列表、實現Item的點擊事件)

ListView的使用小記(使用SimpleAdapter顯示列表、實現Item的點擊事件)

ride simple fma generate and auth int 獲得 static

一、頁面布局

首先創建一個Item.xml的布局文件,此布局是ListView中每個Item的布局

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="#000000"
        android:gravity="center"
        android:text
="TextView" /> <TextView android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/name" android:layout_marginTop="18dp" android:gravity
="center" android:text="TextView" android:textColor="#000000" android:textSize="22sp" /> <TextView android:id="@+id/RSSI" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/address" android:layout_alignParentRight
="true" android:layout_marginRight="52dp" android:gravity="center" android:text="TextView" android:textColor="#000000" android:textSize="22sp" />

技術分享

主頁面布局:

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
         >

    </ListView>

技術分享

二、使用SimpleAdapter作為ListView的適配器

            // TODO Auto-generated method stub
            /*
             * 將數據顯示到listview上
             */
            /**
             * 創建新的適配器
             * Context context:getApplicationContext()
             * List<? extends Map<String, ?>> data:一個以Map為元素的List集合,Map中的鍵值對對應Item.xml的各個控件
             * int resource:指出Item中的布局文件
             * String[] from:與  int[] to 對應,與Map集合裏的key相同
             * int[] to:與 String [] from 對應,與item.xml中的id相同
             */
            SimpleAdapter saAdapter=new SimpleAdapter(getApplicationContext(), 
                                                        Utils.getListOfMap(btlist),
                                                        R.layout.item,
                                                        new String[] {"btName","btRSSI","btAddress"},
                                                        new int [] {R.id.name,R.id.RSSI,R.id.address});
            listView.setAdapter(saAdapter);//設置listview的適配器為上述適配器

其中,第二個參數所謂之“一個以Map為元素的List集合”,可有各個方法得到,如下例方法

    public static List<Map<String, String>> getListOfMap_(){
        List<Map<String, String>> resList=new ArrayList<Map<String,String>>();
        for (int i=0;i<90;i++) {
            Map<String , String> data=new HashMap<String, String>();
            data.put("btName", i+"");
            data.put("btAddress", i+"");
            data.put("btRSSI",i+"");
            resList.add(data);
        }
        return resList;
    }

配置完成之後就會顯示數據

三、ListView之Item點擊事件

寫一個條目被點擊的監聽事件

listView.setOnItemClickListener(new itemClick());

其中 new itemClick() 是以下OnItemClickListener 實現類的對象

    /**
     * listview條目點擊事件的實現類(內部類)
     * @author Administrator
     *
     */
    class itemClick implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            Map<String, String> infoMap = (Map<String, String>) parent.getItemAtPosition(position);
            System.out.println(infoMap.get("btName"));
            System.out.println(infoMap.get("btAddress"));
            System.out.println(infoMap.get("btRSSI"));
        }
        
    }

使用 Map<String, String> infoMap = (Map<String, String>) parent.getItemAtPosition(position); 來獲得你點擊的Item的Map集合,就可以得到Item內的信息。

 

ListView的使用小記(使用SimpleAdapter顯示列表、實現Item的點擊事件)