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






<div class="jd-details-descr" style="padding:0px; margin:0.5em 0.25em; color:rgb(34,34,34); font-family:Roboto,sans-serif; font-size:14px; line-height:19px; background-color:rgb(249,249,249)"><div class="jd-tagdata" style="margin:0.5em 1em"><table class="jd-tagtable          " style="font-size:1em; margin:0px; border-collapse:collapse; border-spacing:0px; border:0px; width:auto; background-color:transparent"><tbody></tbody></table></div></div><pre>
</pre><pre name="code" class="html">
 
 
 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值