• 第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter


    第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter

    1.BaseAdapter

    BaseAdapter是Android应用程序中经常用到的基础数据适配器,它的主要用途是将一组数据传到像ListView、Spinner、Gallery及GridView等UI显示组件,它是继承自接口类Adapter。

    BaseAdapter实现了ListAdapter和SpinnerAdapter两个接口,当然它也可以直接给ListView和Spinner等UI组件直接提供数据。

    利用BaseAdapter构造adapter,需要自定义一个构造类MyAdapter

    一、简单的BaseAdapter

    MainActivity.java部分代码如下:

    private String[] ss=new String[]{"北京1","北京2","北京3","北京4"};


    ListView listView=(ListView)findViewById(R.id.listView1);

    MyAdapter adapter=new MyAdapter(this, ss);

    listView.setAdapter(adapter);

    MyAdapter.java部分代码如下:

    public class MyAdapter extends BaseAdapter{              //自定义一个构造类MyAdapter

    private Context context;

    private String[] ss;

    public MyAdapter(Context context, String[] ss) {

    super();

    this.context = context;

    this.ss = ss;

    }

    // getCount 获取数据源元素个数

    public int getCount() { return ss.length; }

    public Object getItem(int arg0) { return null; }

    public long getItemId(int arg0) {return 0; }

    //需要构建一个View对象,来展示数据源中的信息

    public View getView(int position, View converView, ViewGroup parent){

    String str=ss[position];

    TextView textView=new TextView(context);

    textView.setText(str);

    return textView;

    }

    }

      

    二、复杂的BaseAdapter

    1、首先需要在另一个包package里面定义一个class——User.java

    (User.java中定义了三个参数,实现这三个参数利用快捷键Alt+Shift+s中的构造函数;而要生成get和set函数,利用快捷键Alt+Shift+s中的Getter和Setter。 )

    package com.example.hujun;

    public class User {

    private String name;

    private String phone;

    private Integer age;

    public User(String name, String phone, Integer age) {

    super();

    this.name = name;

    this.phone = phone;

    this.age = age;

    }

    public String getName() {  returnname; }

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

    public String getPhone() {  returnphone; }

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

    public Integer getAge() {  return age; }

    public void setAge(Integer age) {  this.age = age; }

    }

    2、之后在MainActivity.java中,利用自定义的User生成对象user并赋值,将生成的user对象放置在一个list中。

    private List<User> list=newArrayList<User>();

    for (int i = 0; i < 10; i++) {                   //添加一些简单元素

    User user=newUser("hujun"+i, "123", 24+i);

    list.add(user);

    }

    3、然后,为了将user对象放置在布局中,需要自定义一个layout——user_item.xml 分别用三个TextView放置name, phone, age。

    4、由于数据源格式改变了,因此需要改变adapter以接受数据源list

    package com.example.test_listview2;

    public class MyAdapter extends BaseAdapter{

    private Context context;

    private List<User> list;

    public MyAdapter(Context context, List<User> list) {

    super();

    this.context = context;

    this.list = list;

    }

    //获取数据源元素个数

    public int getCount() { return list.size(); }

    public Object getItem(int arg0) { return null; }

    public long getItemId(int arg0) { return 0; }

    //需要构建一个View对象,来展示数据源中的信息

    public View getView(int position, View converView, ViewGroup parent){

    User user=list.get(position);

    LayoutInflater inflater=

    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewGroup group=(ViewGroup) inflater.inflate(R.layout.user_layout,null);

    TextView textView1=(TextView) group.findViewById(R.id.textView1);

    textView1.setText(user.getName());

    TextView textView2=(TextView) group.findViewById(R.id.textView2);

    textView2.setText(user.getPhone());

    TextView textView3=(TextView) group.findViewById(R.id.textView3);

    textView3.setText(String.valueOf(user.getAge()));

    return group;

    }

    }

    5、将新的adapter赋予listView

    listView.setAdapter(newMyAdapter(this,list));

    2.SimpleAdapter

    simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

    作用是ArrayList和 ListView的桥梁。这个ArrayList里边的每一项都是一个Map<String,?>类型。
    ArrayList当中的每一项Map对象都和ListView里边的每一项进行数据绑定一一对应。

    SimpleAdapter(Context context,  List<?extends Map<String, ?>> data,  intresource,  String[] from,  int[] to);
    参数:
    1,context:上下文。
    2,data:基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
    3,resource :就是一个布局layout,可引用系统提供的,也可以自定义。
    4,from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。即 map 中得key值
    5,to:里面是一个TextView数组。这些 TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。

      

    listitem.xml文件:

    <?xmlversion=”1.0″ encoding=”utf-8″?>

    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

    android:orientation=”horizontal” android:layout_width=”fill_parent”

    android:layout_height=”wrap_content”>

    <TextView android:id=”@+id/mview1″

    android:layout_width=”100px”

    android:layout_height=”wrap_content” />

    <TextView android:id=”@+id/mview2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content” />

    </LinearLayout>

    下面是activity文件的部分代码:

             //构造数据部分。

    List<Map<String, Object>> data = newArrayList<Map<String,Object>>();

    Map<String,Object> item;

    item = new HashMap<String,Object>();

    item.put(“姓名”,”张三”);       item.put(“性别”,”男”);

    data.add(item);

    item = new HashMap<String,Object>();

    item.put(“姓名”,”李四”);       item.put(“性别”,”女”);

    data.add(item);

    //构造listview对象。

    ListView listview= new ListView(this);

    //构造一个适配器。

    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.listitem,new String[]{“姓名”,”性别”},

    new int[]{R.id.TextView01, R.id.TextView02});

    /*1,第三个参数是说明用的是自定义的布局R.layout.listtiem。

    * 2,第四和第五个参数一起理解:把我们添加数据时姓名那一列对应到R.id.TextView01这个TextView中,把性别对应到R.id.TextView02这个 TextView中。

    * 如果把from改为new String[]{“姓名”,”姓名”},测试下就会明白。

    */

    listview.setAdapter(adapter);

    setContentView(listview);

  • 相关阅读:
    The connection to adb is down, and a severe error has occured
    android 补间动画
    Android Geocoder(位置解析)
    Android服务之AIDL
    SQL语言学习-数据操纵语言
    SQL语言学习-数据定义语言
    asp.net MVC 验证注解
    django之用户表的继承
    django之ModelForm组件
    django之 基于queryset和双下划线的跨表查询
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171593.html
Copyright © 2020-2023  润新知