码迷,mamicode.com
首页 > 其他好文 > 详细

SimpleAdapter的用法

时间:2014-07-18 16:33:37      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   

bubuko.com,布布扣

 

Mainactivit.java

 1 package com.kale.listview;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import android.app.Activity;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.AdapterView;
12 import android.widget.AdapterView.OnItemClickListener;
13 import android.widget.AdapterView.OnItemSelectedListener;
14 import android.widget.ListView;
15 import android.widget.SimpleAdapter;
16 
17 public class MainActivity extends Activity {
18     
19     ListView myLv;
20     
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         
26         myLv = (ListView)findViewById(R.id.red_listView_id);
27         //定义5个人名数组,5个性别数组,5个图片(图省事,我用一个图片代替了)
28         final String []name = {"Jack","Tony","Tom","Luna","Nacy"};
29         String []sex = {"male","male","male","female","female"};
30         int []pic = {R.drawable.kale,R.drawable.kale,R.drawable.kale,R.drawable.kale,R.drawable.kale};
31         
32         //建立一个list,里面的元素师Map,Map中是String,object
33         List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
34         for (int i = 0; i < name.length; i++) {
35             //建立list中的子元素,用键值对的形式来存放数据
36             Map<String, Object> listItem = new HashMap<String, Object>();
37             listItem.put("picture", pic[i]);
38             listItem.put("name", name[i]);
39             listItem.put("sex", sex[i]);
40             //将子元素添加入list中
41             list.add(listItem);
42         }
43         
44         //建立一个simpleAdapter
45         //1.Context对象,2.list对象,3.item的布局文件,4.各个元素来源的键名,5.各个元素对应控件的id
46         SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, 
47                 new String[] {"picture","name","sex"}, 
48                 new int[] {R.id.pic_imageView_id,R.id.name_textView_id,R.id.sex_textView_id});
49         
50         myLv.setAdapter(adapter);
51         myLv.setOnItemClickListener(new OnItemClickListener() {
52 
53             @Override
54             public void onItemClick(AdapterView<?> arg0, View parent, int position,long id) {
55                 // TODO 自动生成的方法存根
56                 System.out.println("onItemClick"+name[position]+"被选中了");
57                 
58             }
59         });
60         //列表项被选中后的监听器,两个监听器的区别参考这篇文章:http://blog.csdn.net/bcai2/article/details/15028979
61         //就和电视的菜单一样,上下选择来触发选中(selected)事件,按下确定才是点击(click)事件。
62         myLv.setOnItemSelectedListener(new OnItemSelectedListener() {
63 
64             @Override
65             public void onItemSelected(AdapterView<?> parent, View arg1,int position, long id) {
66                 // TODO 自动生成的方法存根
67                 System.out.println("onItemSelected"+name[position]+"被选中了");
68             }
69 
70             @Override
71             public void onNothingSelected(AdapterView<?> arg0) {
72                 // TODO 自动生成的方法存根
73                 
74             }
75         });
76     }
77 }

 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 设置红色分割线的listView -->
    <ListView 
        android:id="@+id/red_listView_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"
        android:dividerHeight="2dp"
        android:layout_gravity="center"
        android:headerDividersEnabled="false"/>
    

</LinearLayout>

 

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="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/pic_imageView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/name_textView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/sex_textView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:textColor="#0000ff"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

SimpleAdapter的用法,布布扣,bubuko.com

SimpleAdapter的用法

标签:android   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/tianzhijiexian/p/3850988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!