首页 > 代码库 > ListView作业

ListView作业

这次的作业没有用Adapter来做,而是在Activity中实现内容的传递,对于Adapter还不行。
1.第一个是ListView列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.list.MainActivity">

    <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list_item">


</ListView>

    </LinearLayout>
 
 
 
 2.显示ListView的数据需要4行来显示数据所以创建四个TextView
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/name" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/age" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/address" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/email" />

    </LinearLayout>
</LinearLayout>
3.在mainActivity中进行数据的传递
package com.example.administrator.list;

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

import java.sql.RowId;
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={25,25,25,25,25};
    private String[] adress={"厦门市","漳州市","厦门市","厦门市","福州市"};
    private String[] email={"ffzk86@gmail.com","aa@bb.com","cc@gmail.com","ccadd@gmail.com","ffczk86@gmail.com"};
    private ListView lv;

    @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("email","邮箱:"+email[i]);
            listems.add(listem);
        }
        SimpleAdapter simplead = new SimpleAdapter(this, listems,R.layout.activity_list, new String[] { "name", "age", "adress","email" },
                new int[] {R.id.name,R.id.age,R.id.address,R.id.email});
        lv=(ListView)findViewById(R.id.list_item);
        lv.setAdapter(simplead);

    }
}
结果图如下:


 技术分享

ListView作业