ListView与适配器的使用

今天打算修改一个drawerLayout的源码时准备修改一个ListView,原版的ListView中的Item均为TextView,然而我想做成ImageView+TextView的。而之前由于突击学习对ListView不甚了解,在修改Item的xml文件后报错。

之后查阅了一下资料,有关ListView和Adapter的使用可以参考此文

http://l62s.iteye.com/blog/1675509

针对此文中讲述的知识,准备编写一个测试项目,暂且mark一记,稍后贴上我自己的源码。


——————————update 一下源代码————————————————————

首先需要一个listItem的xml文件

<RelativeLayout
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <ImageView 
        android:id="@+id/imgButton"
         android:layout_width="16dp"
            android:layout_height="16dp"
           android:src="@drawable/quit"
            android:scaleType="fitXY"
        />
    
    <TextView 
    android:id="@+id/text1"
    android:layout_toRightOf="@id/imgButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
</RelativeLayout>


其次在java文件中需要有以下内容

 mDrawerList = (ListView) findViewById(R.id.left_drawer);
        
        // set a custom shadow that overlays the main content when the drawer opens
        ArrayList<HashMap<String,Object>> listItem = new ArrayList<HashMap<String,Object>>();
        HashMap<String,Object> map = null;
        map = new HashMap<String,Object>();
        map.put("ItemImage", R.drawable.ic_launcher);
        map.put("ItemText", "导航");
        listItem.add(map);
        map = new HashMap<String,Object>();
        map.put("ItemImage", R.drawable.ic_launcher);
        map.put("ItemText", "账户");
        listItem.add(map);
        
        SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.drawer_list_item,new String[] {"ItemImage","ItemText"}, new int[] {R.id.image,R.id.text1});
        
        mDrawerList.setAdapter(mSimpleAdapter);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值