使用两个XML界面:
<?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="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/mail" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/adress" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list1"> </ListView> </LinearLayout>
<?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:orientation="vertical" tools:context="com.example.zhoushasha.myapplication.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list1"> </ListView> </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 String[]mail={"162615162","236272882","173662362","12876216146"}; 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); } }
以上就是实现列表显示的代码。