• 今日小结 4.6


    • ListView控件——OK
    • MainActivity主窗口右上角添加Menu菜单,跳转到添加衣服AddClothesActivity——OK
    • android:style ,toolbar的使用  —— OK

     1.适配器 Adapter 相当于控制器(C),是连接数据模型(M)和视图(V)的桥梁。

    类似于J2EE中的MVC框架,数据模型M(Model)存放数据,利用控制器C(Controller)将数据显示在视图V(View)上。

    从宏观上看,适配器Adapter是接口implements的应用之,它是一个抽象类,连接自定义类和接口,

    也就是让自定义类有选择性的实现接口的功能。他需要被子类继承。

    2.适配器视图类 AdapterView 声明如下:

    public abstract class AdapterView<T extends Adapter> extends ViewGroup

            

    java.lang.Object

    android.view.View

             android.view.ViewGroup

                       android.widget.AdapterView<T extends android.widget.Adapter>

    (1)视图V:AdapterView 是视图类 

    AdapterView中的数据类型——泛型<T extends Adapter 是继承了Adapter的抽象类,

    也就是说适配器视图类AdapterView是内容(M,数据模型)Adapter(C,控制器) 来决定的视图类(V)

    AdapterView的作用是以列表形式显示多项具有相同格式的资源。

    使用AdapterView的好处是他将前端显示和后端数据相分离,(也就是MVC框架),使得视图与数据的绑定更加便捷,便于修改。

    ListView 列表,GridView 网格表,Spinner 下拉列表,Gallery 相册 都是AdapterView的子类。

    (2)控制器C:各种Adapter适配器,绑定数据到适配器,再绑定适配器到视图类

    即 数据 —> 控制器 —> 视图

    Adapter

    含义

    ArrayAdapter<T>

    用来绑定一个数组,支持泛型操作

    SimpleAdapter

    用来绑定在xml中定义的控件对应的数据

    SimpleCursorAdapter

    用来绑定游标得到的数据

    BaseAdapter

    通用的基础适配器

     

     

    (3)数据模型M

    例如:

    public class MainActivity extends AppCompatActivity {
        private String[] clothesName = new String[]{        //数据模型M,数组形式Array,String类型
                "dhu_uniform_blue", "jacket_long_sleeve_blue", "jeans_long_light_blue",
                "jeans_short_dark_blue", "T-shirt_white", "T-shirt_black",
                "jeans_short_dark_blue", "T-shirt_white", "T-shirt_black",
                "jeans_short_dark_blue", "T-shirt_white", "T-shirt_black"
        };
        private ListView lv_showClothesName = null;        //适配器视图V,ListView是AdapterView(适配器视图类V)的一个子类,
        private ArrayAdapter<String>  arrayAdapter = null;      //适配器控制器C,绑定数组的适配器,类似的还有SimpleAdapter,ListAdapter等
                                                                //以上构成了MVC框架
        

     

    2.泛型 —— 给数据类型设置代号,统一数据类型,避免了类转换异常(ClassCastException)

    object类可以接受任何数据类型,但是容易引发安全问题——setter和getter的数据类型不匹配

     

    3.自动装箱,自动拆箱

    自动装箱 是将基本数据类型转化为引用数据类型

    自动拆箱 是反过来

     

    比如,

    (1)装箱

    Integer a = 3;  //自动装箱

    相当于

    Integer a = Integer.valueOf(3);

    (2)拆箱

    int a = new Integer(3);

     

    4.什么是ArrayAdapter

    new ArrayAdapter<T>()

     

    5.哈希表HashMap<>  

    键值对

     

    6.使用SimpleAdapter的步骤

    (1).安排布局

    (2).声明属性

    (3).绑定数据到控制器

    (4).绑定控制器到视图类

            simpleAdapter = new SimpleAdapter(this,arrayList,       //绑定数据到控制器,用到了SimpleAdapter的构造函数完成其实例化
                    R.layout.item_main,
                    new String[] {"itemname","itemcontent","itemimage"},
                    new int[] {R.id.tv_itemname,R.id.tv_itemcontent,R.id.iv_itemimage} );
            lv_clothesLists.setAdapter(simpleAdapter);            //绑定控制器到视图类
            lv_clothesLists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(SimpleAdapterDemo.this,"点击了第 "+ position + 1 +" 行",Toast.LENGTH_SHORT).show();
                }
            });

    7.

    AppCompatActivity

    Toolbar

    8.如果要使用ToolBar,就要先去掉ActionBar状态栏和标题栏Title

        <style name="AppTheme.NoActionBar">
            <item name="android:windowActionBar">false</item>
            <item name="android:windowNoTitle">true</item>
        </style>

     或者直接

        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>

     

  • 相关阅读:
    多层感知机的简洁实现
    汇编语言总结(1)
    回顾下TCP/IP协议
    五笔输入法的前世今生
    大O算法
    算法基础概念
    TCP和UDP的区别
    数据结构基本概念和术语
    gdb调试方法
    GD32F20x系列使用问题总结
  • 原文地址:https://www.cnblogs.com/Chongger/p/5361768.html
Copyright © 2020-2023  润新知