Android 的 第一个ListView

酷今天做出了一个listView 废话不多,直接上代码

1.首先 在man.xml中 添加一个ListView 添加id为ListView_01

代码:<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ListView_01">
    </ListView>

</LinearLayout>
2. 创建一个 listtype.xml 作为 ListView 其中的一项

代码:

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="68dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="50dp"
        android:layout_toRightOf="@+id/image"
        android:text="这是一行测试文字" />
   
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/textView1"
        android:textSize="10dp"
        android:text="这是一行测试文字" />

</RelativeLayout>

3.修改MainActivity.java

package com.Linear;

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

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list = (ListView) findViewById(R.id.ListView_01);

        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("listImage", R.drawable.ic_launcher);
            map.put("listTitle", "Level " + i);
            map.put("listText", "liweishigedashuaige");
            listItem.add(map);
        }
        // 生成适配器 的item 和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// map
                                                                            // 资源
                R.layout.listtype,// 一项的格式
                new String[] { "listImage", "listTitle", "listText" },// Map对象的哪些key对应value来生成列表项
                new int[] { R.id.image, R.id.textView1, R.id.textView2 });// 表示来填充的组件
                                                                            // Map对象key对应的资源一依次填充组件
                                                                            // 顺序有对应关系
        list.setAdapter(listItemAdapter);
    }
}

接下来是

SimpleAdapter的参数说明

 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
 第二个参数表示生成一个Map(String ,Object)列表选项
 第三个参数表示界面布局的id  表示该文件作为列表项的组件
 第四个参数表示该Map对象的哪些key对应value来生成列表项
 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源
 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
这个head的组件会被name资源覆盖

猜你喜欢

转载自15035554141.iteye.com/blog/1959234