关闭

Adapter适配器和ListView

96人阅读 评论(0) 收藏 举报
分类:

一、Adapter

1、Adapter适配器简介


Adapter是连接View(无法直接setValue的View,如ListView)和数据的桥梁,使View和数据之间能够联动


2、Adapter适配器的使用


根据数据源的类型选择使用不同的适配器

(1)ArrayAdapter

ArrayAdapter的数据源为 泛型数组(T[]),项布局只能为<TextView />

Activity代码:

public class AdapterActivity extends AppCompatActivity {
    ListView mContentlistview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter_layout);
        /**
         * 1、实例化需要Adapter联系的View
         */
        mContentlistview = (ListView) findViewById(R.id.content_listview);
        /**
         * 2、构造数据源
         */
        String[] item = {"first", "second", "third", "fourth"};
        /**
         * 3、实例化并且配置适配器
         */
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.adapter_item_layout, item);
        /**
         * 4、View加载Adapter
         */
        mContentlistview.setAdapter(adapter);
    }
}


AdapterActivity布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sjl.schx.person_message.AdapterActivity">

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

项布局代码:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
效果:



(2)SimpleAdapter

SimpleAdapter的数据源为Map类的子类(List<?extends Map>),项布局文件可以完全自定义

Activity代码:

public class SimpleActivity extends AppCompatActivity {
    ListView mSimpleListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_layout);
        /**
         * 1、实例化需要Adapter联系的View
         */
        mSimpleListView = (ListView) findViewById(R.id.simple_listview);
        ArrayList itemList = new ArrayList();//ArrayList的元素必须是MAP的子类
        HashMap<String, Object> item = new HashMap<String, Object>();
        /**
         * 2、构造数据源
         */
        //============设置第一项属性
        item.put("IMG", R.drawable.apple);
        item.put("TITLE", "1.苹果");
        item.put("CONTENT", "\r\r4.0元/斤");
        //===========添加第一项
        itemList.add(item);
		
        //============设置第二项属性
        item = new HashMap<String, Object>();
        item.put("IMG", R.drawable.banana);
        item.put("TITLE", "2.香蕉");
        item.put("CONTENT", "\r\r5.0元/斤");
        //===========添加第二项
        itemList.add(item);
        /**
         * 3、新建配置适配器
         */
        String[] from = {"IMG", "TITLE", "CONTENT"};
        int[] to = {R.id.simple_item_img, R.id.simple_item_titel_txt, R.id.simple_item_content_txt};
        SimpleAdapter adapter = new SimpleAdapter(this, itemList, R.layout.acitivity_simple_item_layout, from, to);
        /**
         * 4、桥接适配器
         */
        mSimpleListView.setAdapter(adapter);
    }
}


SimpleActivity布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    tools:context="com.sjl.schx.person_message.SimpleActivity">

    <ListView
        android:id="@+id/simple_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</RelativeLayout>

项布局代码:


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

    <ImageView
        android:id="@+id/simple_item_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />

    <TextView
        android:id="@+id/simple_item_titel_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/simple_item_img"
        android:text="1.苹果"
        android:textSize="32sp" />

    <TextView
        android:id="@+id/simple_item_content_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/simple_item_titel_txt"
        android:layout_below="@id/simple_item_titel_txt"
        android:text="    XX元/斤"
        android:textSize="24sp" />
</RelativeLayout><strong><span style="font-size:14px;">
</span></strong>

效果:


3、自定义Adapter


1)类实现BaseAdapter接口

public class MyAdapter extends BaseAdapter{}

(2)根据需要重载构造方法

public class MyAdapter extends BaseAdapter {
    Context c;
    List<? extends Map> l;
    int childtLayout;
    String[] key;
    int[] childViews;
    LayoutInflater li;

    public MyAdapter(Context c, List<? extends Map> l, int childtLayout, String[] key, int[] childViews) {
        this.c = c;
        this.l = l;
        this.key = key;
        this.childtLayout = childtLayout;
        this.childViews = childViews;
        li = LayoutInflater.from(c);
    }
(3)重写getCount()、getItem(intposition)、getItemId(intposition)、getView(intposition,ViewconvertView,ViewGroup parent)方法

public class MyAdapter extends BaseAdapter {
    Context c;
    List<? extends Map> l;
    int childtLayout;
    String[] key;
    int[] childViews;
    LayoutInflater li;

    public MyAdapter(Context c, List<? extends Map> l, int childtLayout, String[] key, int[] childViews) {
        this.c = c;
        this.l = l;
        this.key = key;
        this.childtLayout = childtLayout;
        this.childViews = childViews;
        li = LayoutInflater.from(c);
    }

    /**
     * 获取项数
     *
     * @return
     */
    @Override
    public int getCount() {
        return l.size();
    }

    /**
     * 获取每项的元素
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return l.get(position);
    }

    /**
     * 获取每项编号
     *
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取每项View(通常设置内容也在这里进行)
     *
     * @param position    项编号,从0自增
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = li.inflate(childtLayout, null);
        ((ImageView) v.findViewById(childViews[0])).setImageResource((Integer) l.get(position).get(key[0]));
        ((TextView) v.findViewById(childViews[1])).setText((String) l.get(position).get(key[1]));
        ((TextView) v.findViewById(childViews[2])).setText((String) l.get(position).get(key[2]));
        return v;
    }

}


二、ListView列表控件

ListView无法直接加载显示数据,需要通过适配器联系数据

ListView每个项的元素都是一个集合

1、相关事件处理

OnItemClickLietener 项点击事件监听



public class SimpleActivity extends AppCompatActivity {
    ListView mSimpleListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_layout);
        /**
         * 1、实例化需要Adapter联系的View
         */
        mSimpleListView = (ListView) findViewById(R.id.simple_listview);
        ArrayList itemList = new ArrayList();//ArrayList的元素必须是MAP的子类
        HashMap<String, Object> item = new HashMap<String, Object>();
        /**
         * 2、构造数据源
         */
        //============设置第一项属性
        item.put("IMG", R.drawable.apple);
        item.put("TITLE", "1.苹果");
        item.put("CONTENT", "\r\r4.0元/斤");
        //===========添加第一项
        itemList.add(item);
        //============设置第二项属性
        item = new HashMap<String, Object>();
        item.put("IMG", R.drawable.banana);
        item.put("TITLE", "2.香蕉");
        item.put("CONTENT", "\r\r5.0元/斤");
        //===========添加第二项
        itemList.add(item);
        /**
         * 3、新建配置适配器
         */
        String[] from = {"IMG", "TITLE", "CONTENT"};
        int[] to = {R.id.simple_item_img, R.id.simple_item_titel_txt, R.id.simple_item_content_txt};
        MyAdapter myAdapter = new MyAdapter(this, itemList, R.layout.acitivity_simple_item_layout, from, to);
        //SimpleAdapter adapter = new SimpleAdapter(this, itemList, R.layout.acitivity_simple_item_layout, from, to);
        /**
         * 4、桥接适配器
         */
        //  mSimpleListView.setAdapter(adapter);
        mSimpleListView.setAdapter(myAdapter);
        mSimpleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //position为点击的项的编号,从上至下,从0自增
                switch ((position)) {
                    case 0:
                        Snackbar.make(view, "产地:china", Snackbar.LENGTH_LONG).show();
                        break;
                    case 1:
                        Adapter adapter1 = parent.getAdapter();//获取适配器
                        HashMap item1 = (HashMap) adapter1.getItem(position);//获取第一项的元素
                        Snackbar.make(view, item1.get("TITLE").toString(), Snackbar.LENGTH_LONG).show();
                        break;
                }
            }
        });
    }
}

效果:

点击"苹果":


点击“香蕉”:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19066次
    • 积分:606
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条