SimpleAdapter + ListView 使用

本文介绍如何利用Android中的SimpleAdapter实现带有图片和文本的列表视图,并展示了具体的XML布局文件及Java代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现的效果图

 

技术分析:

第一:SimpleAdapter的构造函数

 

第二:list与map形成的数据结构

代码:

主界面aa.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <ListView
 7         android:id="@+id/listView1"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:layout_x="2dp"
11         android:layout_y="2dp" >
12     </ListView>
13 
14 </AbsoluteLayout>

样式界面aaa.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <ImageView
 7         android:id="@+id/MyLogo"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_x="12dp"
11         android:layout_y="12dp"
12         android:src="@drawable/ic_launcher" />
13 
14     <TextView
15         android:id="@+id/MyName"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_x="211dp"
19         android:layout_y="15dp"
20         android:text="Large Text"
21         android:textAppearance="?android:attr/textAppearanceLarge" />
22 
23     <TextView
24         android:id="@+id/MyTel"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_x="212dp"
28         android:layout_y="70dp"
29         android:text="TextView" />
30 
31 </AbsoluteLayout>

 

主程序:

 1 package com.abc;
 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.net.Uri;
 9 import android.os.Bundle;
10 import android.app.Activity;
11 import android.content.Intent;
12 import android.view.Menu;
13 import android.view.View;
14 import android.widget.AdapterView;
15 import android.widget.SimpleAdapter;
16 import android.widget.AdapterView.OnItemClickListener;
17 import android.widget.ListView;
18 
19 public class MainActivity extends Activity implements OnItemClickListener{
20 
21     ListView listview;
22     List<Map<String,Object>> list;
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.aa);
27         //难点二
28         listview=(ListView) this.findViewById(R.id.listView1);
29         listview.setOnItemClickListener(this);
30         
31         
32         list=new ArrayList<Map<String,Object>>();
33         Map<String,Object> m=null;
34         
35         {m=new HashMap<String,Object>();
36         m.put("name", "张三");
37         m.put("tel", "13425252525");
38         m.put("logo",R.drawable.a1);
39         list.add(m);}
40         
41         {m=new HashMap<String,Object>();
42         m.put("name", "李四");
43         m.put("tel", "138123465987");
44         m.put("logo",R.drawable.a2);
45         list.add(m);}
46         
47         {m=new HashMap<String,Object>();
48         m.put("name", "王五");
49         m.put("tel", "13567828765");
50         m.put("logo",R.drawable.a3);
51         list.add(m);}
52         
53         {m=new HashMap<String,Object>();
54         m.put("name", "刘六");
55         m.put("tel", "1842252525");
56         m.put("logo",R.drawable.a4);
57         list.add(m);}
58         
59         
60         //*****难点一******//
61         SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.aaa,
62                     new String[]{"logo","name","tel"}, new int[]{R.id.MyLogo,R.id.MyName,R.id.MyTel});
63         listview.setAdapter(adapter);
64         
65         
66         
67     }
68     @Override
69     public void onItemClick(AdapterView<?> a, View b, int c, long d) {
70         // TODO Auto-generated method stub
71         Map<String,Object> map=list.get(c);  //响应拨号
72         Uri uri=Uri.parse("tel:"+map.get("tel").toString());
73         Intent i=new Intent(Intent.ACTION_DIAL,uri);
74         startActivity(i);
75     }
76 
77     
78 }

 

转载于:https://www.cnblogs.com/seemenlee/p/5390547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值