Android的UI组件之ListView(二)

接着上篇文章:

二. 通过构建Adapter(适配器)来在ListView显示一个组合控件

UI的xml代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <ListView 
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>
	
</LinearLayout>
list.xml是在ListView中的每个Item的布局xml文件,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
     >
     <LinearLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:orientation="vertical" 
         >
         <!-- 定义一个ImageView作为列表项的一部分 -->
	<ImageView 
		android:id="@+id/header"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:paddingLeft="10dp"   
	    />
	<TextView 
	    android:id="@+id/name"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:textSize="16dp"
	    android:gravity="center_vertical"
	    android:paddingLeft="10dp"
	    />
     </LinearLayout>
     <EditText 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="请输入"
         android:layout_gravity="center"
         />
</LinearLayout>
Activity的代码如下:

package com.example.simpleadaptertest;

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

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;

public class SimpleAdapterTest extends Activity {

    private String [] names=new String[] {"虎头","弄玉","李清照","李白"};
    private int [] imageIds=new int[]{R.drawable.tiger, R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_adapter_test);
        //创建一个List集合,List集合的元素是Map
        List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();
		for(int i=0;i<names.length;i++){
			Map<String, Object> listItem=new HashMap<String, Object>();
			listItem.put("header", imageIds[i]);
			listItem.put("personName", names[i]);
			listItems.add(listItem);
		}
		SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.list, new String[]{"personName","header"}, new int[]{R.id.name,R.id.header});
		ListView list=(ListView)findViewById(R.id.myList);
		list.setAdapter(simpleAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_simple_adapter_test, menu);
        return true;
    }

    
}
先看运行结果:

在Activity代码中的第34行:SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.list, new String[]{"personName","header"}, new int[]{R.id.name,R.id.header});中的R.layout.list即显示每个Item中要显示的组合控件以及其布局。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值