Android之Adapter用法总结
1.概念
Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。在常见的View(List View,Grid View)等地方都需要用到Adapter。如下图直观的表达了Data、Adapter、View三者的关系:

Android Adapter详解(1)
Android中所有的Adapter一览:
Android Adapter详解(1)
由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 Base Adapter,Impleader,Adapter,Counteradaptation等。
BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。


1 ArrayAdapter:
最简单的显示一个ListView

((ListView)findViewById(R.id.listView1)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"张三","李四","王五"}));  

同理也可以这样子来写:
private ListView listview ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listview = (ListView) findViewById(R.id.listView1);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);listview.setAdapter(adapter);adapter.add("黎三");adapter.clear();adapter.add("大帅哥");adapter.add("太帅了");adapter.remove("太帅了");}

后者的好处是可以动态的添加

2:SimpleAdapter
这种的用法好处多多,可以应用到很多的场合,图片,文字都没有问题,如果实现以后要完成的功能不是特别复杂的话,都可以应用

package com.example.arrayada;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class SimpleActivity extends Activity implements OnItemClickListener,OnClickListener{private ListView listview ;SimpleAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.simple_main);listview = (ListView) findViewById(R.id.listView2);adapter = new SimpleAdapter(this, getdate(), R.layout.simple_list, new String[]{"img","name","age","sex","nengli"}, new int[]{R.id.img1,R.id.te1,R.id.te2,R.id.te3,R.id.te4});listview.setAdapter(adapter);listview.setOnItemClickListener(this);//findViewById(R.id.but1).setOnClickListener(this);}private List<Map<String, Object>> getdate() {List simplelist = new ArrayList<Map<String, Object>>();Map<String, Object> map = new HashMap<String, Object>();map.put("img", R.drawable.pkq);map.put("name", "皮卡丘");map.put("age", "2");map.put("sex", "男");map.put("nengli", "五星");simplelist.add(map);map = new HashMap<String, Object>();map.put("img", R.drawable.jng);map.put("name", "杰尼龟");map.put("age", "21");map.put("sex", "男");map.put("nengli", "五星");simplelist.add(map);map = new HashMap<String, Object>();map.put("img", R.drawable.xhl);map.put("name", "小火龙");map.put("age", "3");map.put("sex", "男");map.put("nengli", "五星");simplelist.add(map);map = new HashMap<String, Object>();map.put("img", R.drawable.zz);map.put("name", "妙蛙种子");map.put("age", "2");map.put("sex", "男");map.put("nengli", "五星");simplelist.add(map);return simplelist;}@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Map<String, Object> map = (Map<String, Object>) adapter.getItem(arg2);Toast.makeText(this, map.get("name")+"", Toast.LENGTH_SHORT).show();}@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.but1:break;default:break;}}}

<LinearLayout 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:orientation="horizontal"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/img1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:src="@drawable/ic_launcher" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="2"        android:orientation="vertical" >        <TextView            android:id="@+id/te1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="TextView" />        <TextView            android:id="@+id/te2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="TextView" />    </LinearLayout>        <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="2"        android:orientation="vertical" >        <TextView            android:id="@+id/te3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="TextView" />        <TextView            android:id="@+id/te4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="TextView" />    </LinearLayout>    </LinearLayout>

剩下的两种Adapter可以放到下一次讲解中去

更多相关文章

  1. Android Studio gradle配置详解
  2. Android file类使用详解-SDcard
  3. Android:layout_weight详解
  4. android Parcelable接口序列化对象
  5. Android activity 详解一:activity的生命周期

随机推荐

  1. Android中的GridView图片异步加载
  2. js判断移动端系统
  3. android真实项目教程(一)——App应用框架
  4. Android 定义全局 ToastUtils
  5. android proguard 错误处理
  6. Android实现多个TextView同时显示跑马灯
  7. Fix potential event handling issue in
  8. Android 获取View宽高
  9. Wakelock API详解
  10. Android实现google消息通知