1.2列表显示的三元素:数据、视图、适配器

 

 

一.列表显示的三元素:数据、视图、适配器

1.ListView这个组件用于显示;

2.适配器用于绑定数据,即将数据映射到ListView上;

3.数据需映射到ListView的数据可以是字符串、图片或者基本的组件。

二.一般R文件报错,有很大的可能是layout的布局文件出错,记得保存后在build-clean project中clean一下,恢复文件。

:适配器的类型:

1.ArrayAdapter  只能显示一行字。

2.SimpleAdapter 有最好的扩充性,可以定义出各种各样的布局出来,可以放ImageView 、Button 、CheckBox 等。使用时直接继承ListActivity 。

3.SimpleCursorAdapter可以方便的把数据库的内容以列表的形式展示出来。

特别注意:使用SimpleAdapter的数据一般都是HashMap构成的List ,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应的id的组件上。

1.建立两个布局文件tuyang_layout.xml(这里面相当于一个模板,即将要显示在listView里面的版式)和tuyang_list.xml(建立一个listView并声明id,有时候虚拟机不显示列表的items,就在点击Design上方的NoActionBar选择DeviceDefault中的DeviceDefault.DialogWhelarge.NoActionBar)

tuyang_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFCC99"
    >
    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:background="@mipmap/f"
        android:layout_margin="10dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp"
        >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名"
            android:textSize="30dp"/>
        <TextView
            android:id="@+id/intro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="人物简介:集美貌与智慧于一身的大美女。"
            android:textSize="20dp"/>
    </LinearLayout>



</LinearLayout>

2.tuyang_list.xml:

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

3.建立tuyang_listActivity的java文件

 

package com.example.administrator.teachers_app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

/**
 * Created by Administrator on 2016/9/21.
 * 定义列表listview, listView= (ListView) findViewById(R.id.tylist);将该列表的参数传递给listview
 * 对象,然后要把tuyang_layout.xml里的数据格式(list嵌套map:把map里的数据格式以多条的条状显示)封装刀tuyang_list.xml,在用适配器将所有参数传递到listview里
 */
public class tuyang_listActivity extends AppCompatActivity{
    ListView listView;
    List<Map<String,Object>> list=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tuyang_list);
        listView= (ListView) findViewById(R.id.tylist);
        list=getdata();//现在所有的数据都在list这个对象里,下面要做的就是通过普通适配器,把数据送到listview
        // 里进行排列
        SimpleAdapter simpleAdapter=new SimpleAdapter(tuyang_listActivity.this,getdata(),R.layout.
                tuyang_layout,new String[]{"f_image","f_name","f_intro"},
                new int[]{R.id.image,R.id.name,R.id.intro});
        listView.setAdapter(simpleAdapter);//获取适配器的数据
    }

    /**
     * 封装数据
     *
     * @return
     */
    public List<Map<String,Object>> getdata(){
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=new HashMap<>();
        map.put("id","1");
        map.put("f_image",R.mipmap.f);
        map.put("f_name","");
        map.put("f_intro","一个集美貌与智慧于一身的女子");
        list.add(map);
        map=new HashMap<>();
        map.put("id","2");
        map.put("f_image",R.mipmap.f);
        map.put("f_name","");
        map.put("f_intro","一个跟猪一样傻乎乎的女子");
        list.add(map);
        map=new HashMap<>();
        map.put("id","3");
        map.put("f_image",R.mipmap.f);
        map.put("f_name","");
        map.put("f_intro","一个温婉持家的女子");
        list.add(map);
        map=new HashMap<>();
        map.put("id","4");
        map.put("f_image",R.mipmap.f);
        map.put("f_name","");
        map.put("f_intro","一个集上述优点于一身的女子");
        list.add(map);
        return list;
    }

}

 

转载于:https://www.cnblogs.com/cmslby/p/5894398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值