simpleAdapter和 ListView的item的定义

本文详细介绍了如何使用SimpleAdapter适配器在Android中实现ListView的自定义项显示,包括适配器的基本用法、数据源的组织、布局文件的定义以及如何将数据与UI元素绑定。

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

1.创建一个工程 ListViewActivity.Java和listview


效果图是什么样的


开始有些疑问为什么是这样的,老师说要用到适配器

用适配器夹在自定义的item样式

那么看一下simpleAdapter适配器的用法

public class ListViewActivity extends Activity {
	 	
	ArrayList<Map<String, Object>> list;
	//数据源
	String appString[] ={"微信","手机QQ","微博","陌陌","飞信","空间"};
	int appId[]={R.drawable.wx,R.drawable.qq,R.drawable.wb,
			R.drawable.mm,R.drawable.fx,R.drawable.kj};
	//适配器
	SimpleAdapter simpleAdapter;
	@ViewInject(R.id.list)
	ListView listView;
	
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.listview); 
			ViewUtils.inject(this);
			list=new ArrayList<Map<String,Object>>();
			for (int i = 0; i <appString.length; i++) {
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("img", appId[i]);
				map.put("text", appString[i]);
				list.add(map);
			}
			
			simpleAdapter=new SimpleAdapter(this, list, R.layout.list_item, 
					new String[]{"img","text"},new int[]{R.id.imgItem,R.id.textItem});
			listView.setAdapter(simpleAdapter);
		}
}

我们可以看到 simpleadapter

1.第一个参数是上下文对象

2.list是定义的ArrayList<Map<String, Object>>集合

3.是item,要加载的到ListView视图的样式

4.5是将item样式里的要修改的放到Map的键当中


item样式


<?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/imgItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="20dp"
        android:src="@drawable/qq"/>

    <LinearLayout
        android:id="@+id/lineText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dip"
            android:text="手机QQ" />

        <TextView
            android:id="@+id/textbelow"
            android:layout_marginTop="2dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="77M|123434下载次数" />

    </LinearLayout>
    
    <Button 
        android:id="@+id/btn" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:text="下载"/>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值