LisView

界面布局

*界面布局的编写

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/title1"
            android:text="@string/app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/title2"
            android:text="@string/nianli"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/title3"
            android:text="@string/dizhi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/title4"
            android:text="@string/youxiang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

java代码部分

package com.example.zhoushasha.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

public class MainActivity extends AppCompatActivity {
    private String[] name={"张学友","刘德华","黎明","郭富城","林志颖"};
    private int [] age={11,20,45,29,24};
    private String[] adress={"香港","广州","深圳","厦门","澳门"};
    private ListView list1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Map<String, Object>> listems = new ArrayList<>();
        for (int i = 0; i < name.length; i++) {
            Map<String, Object> listem = new HashMap<>();
            listem.put("name", "姓名:"+name[i]);
            listem.put("age", "年龄:"+age[i]);
            listem.put("adress", "地址:"+adress[i]);
            listem.put("mail","邮箱:"+mail[i]);
            listems.add(listem);
        }
        SimpleAdapter simplead = new SimpleAdapter(this, listems,R.layout.activity_show, new String[] { "name", "age", "adress","mail" },
                new int[] {R.id.name,R.id.age,R.id.adress,R.id.mail});
        list1=(ListView)findViewById(R.id.list1);
        list1.setAdapter(simplead);
    }
}
posted @ 2017-05-02 22:21  孙耀威  阅读(117)  评论(0编辑  收藏  举报