zoukankan      html  css  js  c++  java
  • Android 各类Adapter总结

    public interface

    Adapter


    1. ListAdater


    public interface ListAdapter implements Adapter



    简介:

    ArrayAdapter比较简单,但它只能用于显示文字。

    而SimpleAdapter则有很强的扩展性,可以自定义出各种效果。

    SimpleCursorAdapter则可以从数据库中读取数据显示在列表上。

    通过从写BaseAdapter可以在列表上加处理的事件等。


    ① ArrayAdapter

    构造方法:

    public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

    Added in API level 1

    Constructor

    Parameters
    context The current context.
    resource The resource ID for a layout file containing a layout to use when instantiating views.
    textViewResourceId The id of the TextView within the layout resource to be populated
    objects The objects to represent in the ListView.

    public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

    Added in API level 1

    Constructor

    Parameters
    context The current context.
    textViewResourceId The resource ID for a layout file containing a TextView to use when instantiating views.
    objects The objects to represent in the ListView.

    T[] objects 也可换为 List<T> objects

    textViewResourceid:资源id或者 TextView的id,一般布局文件为:

    android.R.layout.simple_list_item_single_choice  <!-- 带选择的列表 -->
    android.R.layout.simple_list_item_1  <!-- 显示一个TextView 的列表 -->

    实例代码:

    	ListView listView;
    	private String[] data = { "Android:eoeAndroid.com", "eoeAndroid:",
    			"eoeInstaller", "eoeDouban", "eoeWhere",
    			"eoeInfoAssistant", "eoeDakarGame","eoeTrack" };
            ......
            listView = new ListView(this);
    	listView.setAdapter(new ArrayAdapter<String>(this,
    				android.R.layout.simple_list_item_1, data));
            listView.setAdapter(new ArrayAdapter<String>(this,
    		android.R.layout.simple_list_item_single_choice, data));	
            listView.setItemsCanFocus(true);
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    		setContentView(listView);




    ② SimpleAdapter

    构造方法:


    public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    Added in API level 1

    Constructor

    Parameters

    context

     The context where the View associated with this SimpleAdapter is running

    data

     A List of Maps. Each entry in the List corresponds to one row in the list. 

    The Maps contain the data for each row, and should include all the 

    entries specified in "from"

    resource

    布局文件Resource identifier of a view layout that defines 

    the views for this list item. The layout file should include at 

    least those named views defined in "to"

               from                             

    即需要显示的 Map 中的 Key  ,A list of column names that

     will be added to the Map associated with each item.

    to

    fromKey 中对应的 View 的 id ,The views that should 

    display column in the "from" parameter. These should all be 

    TextViews. The first N views in this list are given the values of 

    the first N columns in the from parameter.


    参数说明:
    from:map中的key
    to:map中key对应的value 要显示在Layout中的Views的id。

    实例说明:
    列表中的内容为 Map 映射的
    新建 Map<String,Objects> 的 Map 对象,添加 key/value 对。
    在创建 List<? extends Map<String,Objects>> 的 对象 列表,添加 Map 对象。
    把 List 对象 添加到 Adapter 中,

    SimpleAdapter adapter = new SimpleAdapter(this, data,
    R.layout.list_item, new String[] { "姓名", "性别" },
    new int[] {R.id.mview1, R.id.mview2 });
    为 ListView 对象添加适配器。 
    listview.setAdapter(adpter);
    listview.setOnItemClickListener(listener);


    ③ ​BaseAdapter


    public abstract classBaseAdapter

    extends Object
    implements ListAdapterSpinnerAdapter

    java.lang.Object

       ↳

    android.widget.BaseAdapter

     

    Known Direct Subclasses

    ArrayAdapter<T>, CursorAdapterSimpleAdapter

     

    Known Indirect Subclasses

    ResourceCursorAdapterSimpleCursorAdapter






    
    
    
    
    
    
    
    
  • 相关阅读:
    mysql分区
    pl/sql查看tnsnames.ora文件路径
    mysql之事务控制和锁定语句
    mysql返回查询行数fund_rows()
    mysql之视图
    Mysql之btree索引和hash索引的区别
    Java中反射 API 的使用
    zookeeper 客户端 Apache curator 分布式锁实操
    springBoot 整合 ZooKeeper Java客户端之 Apache Curator 实战
    初识 ZooKeeper
  • 原文地址:https://www.cnblogs.com/wangmingshuo/p/3323403.html
Copyright © 2011-2022 走看看