前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >simpleAdapter的简单使用

simpleAdapter的简单使用

作者头像
Dream城堡
发布2018-12-28 15:47:47
1.7K0
发布2018-12-28 15:47:47
举报
文章被收录于专栏:Spring相关Spring相关

simpleAdapter的简单使用

项目的结构图如下所示:

image

activity_main.xml:
代码语言:javascript
复制
<?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:
代码语言:javascript
复制
<?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:
代码语言:javascript
复制
<?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:
代码语言:javascript
复制
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:
代码语言:javascript
复制
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 + '\'' +
                '}';
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • simpleAdapter的简单使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档