`

Android Adapter详解(1)

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


Android中所有的Adapter一览:

由图可以看到在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 ;
	
	@Override
	protected 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;
	
	@Override
	protected 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;
	}

	@Override
	public 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();
	}

	@Override
	public 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可以放到下一次讲解中去
  • 大小: 26.6 KB
  • 大小: 55 KB
分享到:
评论

相关推荐

    android adapter详解

    android adapter适配器详解

    android开发Adapter详解

    android开发Adapter详解,帮助你加深进一步的理解···

    Android adapter详解

    个人整理的关于adapter的详细讲解,文章图文并茂,从简单到复杂,从原理到实践。让你更好更快的理解adapter。

    Android Adapter详解(2)

    NULL 博文链接:https://18767136122.iteye.com/blog/1995973

    Android listview与adapter详解及实例代码

    本文主要介绍Android listview与adapter的知识详解,这里整理了相关资料及实现代码和实现效果图,有兴趣的小伙伴可以参考下

    Android Adapter里面嵌套ListView实例详解

    主要介绍了Android Adapter里面嵌套ListView实例详解的相关资料,这里提供实例代码并说明如何实现该功能,需要的朋友可以参考下

    Android基础知识详解

    自定义Adapter来建立复杂的列表项 99 动态添加、删除ListView列表项 102 改变ListView列表项选中状态的背景颜色 102 可展开的列表组件 102 数据的存取 103 SharePreferences 6.1 103 文件的存储6.2 103 SQLite数据库...

    Android RecyclerView网格布局(支持多种分割线)详解(2)

    上篇Android RecyclerView 详解(1)—线性布局 记录了下RecyclerView的使用方法,并且讲述了线性布局列表的使用方法,在此基础上加上了万能分割线,支持颜色分割线和图片分割线,同时支持对分割线设置线宽。 这篇...

    Android ListView

    Android ListView 详解,适配器adapter详解 博客地址:http://blog.csdn.net/csdnyuandaimaxuexi/article/details/48808303

    Android学习资料

    收集的一些关于Android的学习...Android之Adapter用法总结,Android中图片的处理,BaseExpandableListAdapter的使用,反编译android app,详解 Android 的 Activity 组件,需要的朋友可以下载查看(直接双击html文件查看即可)

    Android ListView的OnItemClickListener详解

    我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget.AdapterView.OnItemClickListener。本文主要在于对OnItemClickListener的position和id参数做详细的解释,我相信有些人在这上面走了些...

    MultiItem用法与详解-优雅的实现多类型RecyclerView Adapter

    参考 MultiItem用法与详解-优雅的实现多类型RecyclerView Adapter(https://www.jianshu.com/p/7dc7c8201a90),在此感谢作者。我在MyInputDemoActivity中又添加了表单项验证、弹出popwindow的表单项、省市区三级...

    Android ListView适配器(Adapter)优化方法详解

    Android ListView的优化,在做Android项目的时候,在用到ListView 界面及数据显示,这个时候如果资源过大,对项目来说,用户体验肯定是不好的,这里就对如何优化做了详细介绍: Adapter的作用就是ListView界面与数据...

    Android SimpleAdapter适配器使用详解

    Android SimpleAdapter使用详解 HolderAdapter背景 Android的AdapterView用的比较多,ListView,GridView,Spinner等,原生的BaseAdapter对ViewHolder没有支持,每次都要,定义内部类,inflater根布局,对item内部...

    简单好用的Adapter---ArrayAdapter详解

    主要介绍了简单好用的Adapter---ArrayAdapter详解,具有一定参考价值,需要的朋友可以了解下。

    android studio 的下拉菜单Spinner使用详解

    Spinner 与 Gallery 都继承了AbsSpinner,AbsSpinner 继承了AdapterView,因此它也表现出AdapterView的特征:只要为AdapterView提供Adapter即可。 1.相关属性 android:dropDownHorizontalOffset:设置列表框的水平...

    Android代码-MultiType

    English Version | 《Android 复杂的列表视图新写法 · 详解篇》 Previously, when we need to develop a complex RecyclerView/ListView, it is a boring and troublesome work. We should override the ...

Global site tag (gtag.js) - Google Analytics