android之SimpleAdapter

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" />

   

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="56dp"
  >

    <ImageView
        android:src="@drawable/ic_launcher"
        android:layout_width="48dp"
        android:layout_height="48dp"
       />
    <TableLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
       	android:layout_weight="1.0"
        android:layout_gravity="center_vertical"
        android:stretchColumns="0,1"
        >
        <TableRow >
            <TextView android:id="@+id/tv_01"/>
            <TextView android:id="@+id/tv_02"/>
        </TableRow>
        <TableRow >
            <TextView android:id="@+id/tv_03"/>
            <TextView android:id="@+id/tv_04"/>
        </TableRow>
        
    </TableLayout>

</LinearLayout>

activity

package com.ghg.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

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

public class Day0604_SimpleAdapterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }
    //构造数据源,一个存放map集合的ArrayList
    ArrayList<HashMap<String, Object>> list;
    private   ArrayList<HashMap<String, Object>> getList(){
    	list=new ArrayList<HashMap<String,Object>>();
    	for(int i=1;i<=20;i++){
    		HashMap<String, Object> map=new HashMap<String, Object>();
    		//姓名,身高(cm),体重(斤),价格(万)
    		map.put("name", "jiajia"+i);
    		map.put("height", 160+new Random().nextInt(10));
    		map.put("weight", 100+new Random().nextInt(10));
    		map.put("price", 100+new Random().nextInt(200));
    		list.add(map);
    	}
    	
		return list;
    	
    }
    ListView listView;
	private void initView() {
		// TODO Auto-generated method stub
		listView=(ListView) findViewById(R.id.listView);
		String[] from={"name","height","weight","price"};
		int[] to={R.id.tv_01,R.id.tv_02,R.id.tv_03,R.id.tv_04};
		SimpleAdapter adapter=new SimpleAdapter(this, getList(), R.layout.item, from, to);
		listView.setAdapter(adapter);
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值