• 使用LIstView和自定义Adapter完成列表信息显示


    使用两个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);
        }
    }

    以上就是实现列表显示的代码。

  • 相关阅读:
    https-->http and http-->https bitransfer
    socat
    docker daemon configuration
    centos 7 update to python V3.43 to assure git clone as usual
    df and du
    SS iproute2,nslookup,dig
    jmxtrans
    web.xml之env-entry
    Java发送Http请求
    elasticsearch在CentOS环境下开机启动
  • 原文地址:https://www.cnblogs.com/zhoushasha/p/6798349.html
Copyright © 2020-2023  润新知