ListView添加图片和文字效果之SimpleAdapter简单实例

本文介绍了如何在Android应用中使用SimpleAdapter将图片和文字结合,展示在ListView上。通过SimpleAdapter的构造函数,结合Map数据结构和自定义布局资源文件,实现列表项的个性化显示。

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

转载请注明出处!!!

simpleAdapter的构造函数 如下:

SimpleAdapter(Context context, List <? extends Map <String, ?>> data, int resource, String[] from, int[] to) 


参数context上下文,一半用this,表示在该组件上显示

参数data,表示生成一个Map(String ,Object)列表选项,即要加载到ListView中的数据

参数resource  listview布局文件

from和to表示用key-value形式存放数据,通过这个将数据连接起来


下面贴上实例代码:ListViewItem.java
public class ListViewItem extends Activity {
	private ListView shapeList;
	private SimpleAdapter simpleAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.shapeList = (ListView)super.findViewById(R.id.shapelist);
		
		simpleAdapter = new SimpleAdapter(this, getData(), R.layout.shape_list, 
				new String[]{"content", "image"}, 
				new int[] {R.id.content, R.id.shape_img});
		
		this.shapeList.setAdapter(this.simpleAdapter);//用simpleAdapter适配器填充listview
		
	}
	
	private List<Map<String, Object>> getData() {
		String[] items = getResources().getStringArray(R.array.drawings);
	    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	    
	    //Map 以key-value 的形式存放    HashMap无序存放,key不允许重复
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("content", items[0]);
        map.put("image", R.drawable.draw);
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("content", items[1]);
        map.put("image", R.drawable.straight_line);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("content", items[2]);
        map.put("image", R.drawable.rectangle);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("content", items[3]);
        map.put("image", R.drawable.ellipse);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("content", items[4]);
        map.put("image", R.drawable.circle);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("content", items[5]);
        map.put("image", R.drawable.points);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("content", items[6]);
        map.put("image", R.drawable.brokenline);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("content", items[7]);
        map.put("image", R.drawable.bezier);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("content", items[8]);
        map.put("image", R.drawable.triangle);
        list.add(map);
       
        return list;
	}
}
主配置文件:shape_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:background="@drawable/background"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/shapelist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
要显示内容的配置文件:shape_list.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" >

    <ImageView
        android:id="@+id/shape_img"
        android:layout_width="40dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

</LinearLayout>

然后是实例演示效果




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值