simpleAdapter的简单使用

simpleAdapter的简单使用

项目的结构图如下所示:

image

activity_main.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=".MainActivity">

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

</LinearLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="aaadd"
        />
    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="bbbbb"
        android:textSize="20sp"
        android:textColor="#E52E28"
        />
</LinearLayout>
litepal.xml:
<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="PersonStore"></dbname>
    <version value="1" />

    <list>
        <mapping class="com.example.administrator.simpleadapter.Person"></mapping>
    </list>
    <!--<storage value="internal" />&lt;!&ndash;上线时注意环境是否修改&ndash;&gt;-->

</litepal>
MainActivity:
package com.example.administrator.simpleadapter;

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

import org.litepal.crud.DataSupport;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.找到控件
        ListView lv = findViewById(R.id.lv);
        //2.准备显示的数据
        List<Map<String, String>> data = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("name", "张飞");
        map1.put("phone", "1388888");

        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "赵云");
        map2.put("phone", "1105544555");

        Map<String, String> map3 = new HashMap<>();
        map3.put("name", "貂蝉");
        map3.put("phone", "138858888");

        Map<String, String> map4 = new HashMap<>();
        map4.put("name", "关羽");
        map4.put("phone", "1588445555");

        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);

        //先新增Peroson
        Person person = new Person();
        person.setName("吕布");
        person.setPhone("5656565454");
        person.save();

        List<Person> personList = DataSupport.findAll(Person.class);
        Map<String, String> map5 = new HashMap<>();
        for (Person person1 : personList) {
            map5.put("name", person1.getName());
            map5.put("phone", person1.getPhone());
        }
        data.add(map5);



        //3.设置数据适配器
        //第三个参数 from  map集合的key名称 他会一句这个填充value
        //第四个参数 to 把数据对应放到哪里去
        SimpleAdapter adapter = new SimpleAdapter(
                this,
                data,
                R.layout.item,
                new String[]{"name", "phone"},
                new int[]{R.id.tv_name, R.id.tv_phone}
        );

        lv.setAdapter(adapter);


    }
}
Person:
package com.example.administrator.simpleadapter;

import org.litepal.crud.DataSupport;

public class Person extends DataSupport {

    private String name;
    private String phone;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券