首页 > 代码库 > Listview列表

Listview列表

要想实现输出内容的显示,就得给它们一个输出空间

首先是xml的布局

技术分享
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.list.listshow">
    <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/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/name" />
</LinearLayout>
</LinearLayout>
View Code

然后是MainActivity

技术分享
public class MainActivity extends AppCompatActivity {
//先声明我要里面添加的信息和对ListView进行初始化
    private String[] name={"王","千","大","哥"};
    private int [] age={25,30,15,19,25};
    private String[] adress={"江宁区","栖霞区","鼓楼区","江北区"};
    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", "年龄:"+age[i]);
            listem.put("age", "地址:"+adress[i]);
            listem.put("adress", "姓名:"+name[i]);
            listems.add(listem);
        }//运用for循环进行挨个装入
        SimpleAdapter simplead = new SimpleAdapter(this, listems,R.layout.activity_listshow, new String[] { "name", "age", "adress" },
                new int[] {R.id.name,R.id.age,R.id.address});
        lv=(ListView)findViewById(R.id.list_item);
        lv.setAdapter(simplead);
         //用简单适配器将数据映射到ListView上
    }
}
View Code

结束

Listview列表