第二章实例:SimpleAdapter结合listview实现列表视图

package test.simpleAdapter;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.simpleadapters.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListContent extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.list_content);
        //-----------------------------------------
        //为simpleAdapter构造数据
        String []title = {"排山倒海","葵花点穴手","如来神掌"};
        String []tips  = {"威力巨大给对手造成巨大内伤,即使对手逃逸也会无疾而终","短暂制瘫对方,瞬间废除对手活动能力,即可为所欲为","有效杀伤力达几百米寸草不生,但灰尘太大不环保"};
        int []icon = {R.drawable.psdh,R.drawable.khdxs,R.drawable.rlsz};
        String []time ={"14:30","14:20","14:10"};
        //---------------------------------------
        //构造玩上述数据后,开始构造它们xml布局
        //---------------------------------------
        //将数据打包成MAP对象
        List<Map<String,Object>> map_content = new ArrayList<Map<String,Object>>();
        for(int i = 0; i < title.length;i++){
            Map map = new HashMap<String, Object>();
            //打包方式 key value键值对形式
            map.put("title", title[i]);
            map.put("tips", tips[i]);
            map.put("icon", icon[i]);
            map.put("time", time[i]);
            //------------------------------------
            map_content.add(map);
        }
        //----------------------------------------
        //生成xml布局资源ID引用
        
        int []xmlres = {R.id.avatar,R.id.list_title,R.id.list_tips,R.id.list_time};
        //生成键素组
        String []mapkey = {"icon","title","tips","time"};//与上面的xmlres数组的资源ID对应
        //map对象构造完毕,接下来生成simpleAdapter对象
        //构造simpleAdapter所需参数说明:context窗口对象,盛装map的list,列表栏目布局资源ID的引用,list中的map中对应的键名称,键名称对应的控件资源ID;
        SimpleAdapter sAdapter = new SimpleAdapter(ListContent.this, map_content, R.layout.simple_adapter_list, mapkey, xmlres);
        ListView list = (ListView)findViewById(R.id.main_list);
        list.setAdapter(sAdapter);//设置资源
        
        
        
    }
    
    

}

xml:

//list_content
<?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="vertical" >
    <ListView 
        android:id="@+id/main_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
    </ListView>

</LinearLayout>
//simple_adapter
<?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="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <ImageView 
            android:id="@+id/avatar"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_weight="0.7"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:orientation="vertical" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <TextView 
                    android:id="@+id/list_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.2"/>
                <TextView 
                    android:id="@+id/list_time"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.8"/>
            </LinearLayout>
            <TextView 
                    android:id="@+id/list_tips"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

 

转载于:https://www.cnblogs.com/homes/p/5178640.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值