N_Android

Adapter

https://www.runoob.com/w3cnote/android-tutorial-adapter.html

Adapter 概念

  • Model:通常可以理解为数据,负责执行程序的核心运算与判断逻辑,,通过view获得用户 输入的数据,然后根据从数据库查询相关的信息,最后进行运算和判断,再将得到的结果交给view来显示
  • view:用户的操作接口,说白了就是GUI,应该使用哪种接口组件,组件间的排列位置与顺序都需要设计
  • Controller:控制器,作为model与view之间的枢纽,负责控制程序的执行流程以及对象之间的一个互动

在MVC模型中 Adapter 这个Controller的部分: Model(数据) - Controller(以什么方式显示到)- View(用户界面) 这就是简单MVC组件的简单理解!

Adapter 继承结构

  • BaseAdapter:抽象类,实际开发中我们会继承这个类并且重写相关方法,用得最多的一个Adapter!
  • ArrayAdapter:支持泛型操作,最简单的一个Adapter,只能展现一行文字~
  • SimpleAdapter:同样具有良好扩展性的一个Adapter,可以自定义多种效果!
  • SimpleCursorAdapter:用于显示简单文本类型的listView,一般在数据库那里会用到,不过有点过时, 不推荐使用!

实例

容器布局 (container)

map_layers_list.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
	android:orientation="vertical"  
	android:layout_width="match_parent"  
	android:layout_height="match_parent">  
	<ListView  
		android:id="@+id/listview"  
		android:layout_width="match_parent"  
		android:layout_height="wrap_content" />  
</LinearLayout>
 

var listview = findViewById<ListView>(R.id.listview)

ListView 则是将要应用 Adapter 的容器

内容布局 (item)

map_layers_list_item.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">
 
        <CheckBox
            android:id="@+id/show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="显示" />
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="name" />
        <ImageButton
            android:id="@+id/but_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/stat_sys_download" />
        <ImageButton
            android:id="@+id/but_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/stat_sys_upload" />
</LinearLayout>

Adapter 关联(SimpleAdapter)

	//容器
	bottomSheetDialog!!.show();
	var listview = bottomSheetDialog!!.findViewById<ListView>(R.id.listview)
	/**
	数据结构
	[{"checked":true,"id":"9f149c8228e94db9a6dba36ccf0275a1","name":"坡型图"},{"checked":true,"id":"26e5c5581f044831ab2c8a7521fda660","name":"坡向图"}]
	*/
	val listMap = JacksonUtils.jsonToListMapBean(json);
	/*
	对应 数据键 对 ui id
	*/
	val from = arrayOf("checked", "name")
	val to = intArrayOf(R.id.show, R.id.name)
	/**
	listview 关联  Adapter
	*/
	//val adapter = SimpleAdapter(this, listMap,R.layout.map_layers_list_item, from, to);
	val adapter = object: SimpleAdapter(this, listMap,R.layout.map_layers_list_item, from, to){  
		override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {  
			val view = super.getView(position, convertView, parent)  
			//查找Adapter内的UI组件
			val button: ImageButton = view.findViewById(R.id.but_up)  
			//监听事件
			button.setOnClickListener {}
			// 注意 CheckBox  组件是复用的, 真XX恶心; 用 ClickListener 曲线救国
			val show : CheckBox = view.findViewById(R.id.show)  
			var data = listMap.get(position) as LinkedHashMap  
			show.isChecked = data["checked"] as Boolean  
			show.setOnClickListener{ _->  
				data["checked"] = !(data["checked"] as Boolean)  
				Log.i(TAG," getView 配置结果: "+ listMap)  
				notifyDataSetChanged()  
			}
 
			return view  
		}  
	
	}
	
	if (listview != null) {
		listview.adapter = adapter
	}