Array Simple SimpleCursor Adapter区别
Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View
因为layout五花八门,很多数据都不能直接绑定上去,所以Android引入了Adapter这个机制作为复杂数据的展示的转换载体,所以各种Adapter只不过是转换的方式和能力不一样而已,没什么大不了的
不多说,今天来看下几种常用的Adapter:
数据
源不同则适配器不同
1. String[]: ArrayAdapter
-
ArrayAdapter
-
SimpleAdapter
-
SimpleCursorAdapter
-
SimpleExpandableListAdapter
-
SimpleCursorTreeAdapter
例子就不贴了,API DEMO里大把是,自己copy去,我就说下我的理解和他们的区别
ArrayAdapter:顾名思义,专门负责将数组结构的数据适配进view中的,最简单,常用于demo...⊙﹏⊙b汗...和Spinner
,AutoCompleteTextView
中
SimpleAdapter:从名字上看不出什么所以然,其他这个东西很给力,在正常情况下他的灵活性最好,扩展性也最强(ViewBinder) ListView
中
SimpleCursorAdapter:拥有上者的扩展性和灵活性,同时可以将Cursor进行适配
SimpleExpandableListAdapter:这玩意就漏也了,只能适配到TextView上,在简单的UI中可以为ExpandableListView提供数据
SimpleCursorTreeAdapter:这是Adapter的终极Boss...-_-|||... ViewBinder+Cursor+Expandable...三位一体
2. 数据库
Cursor: SimpleCursorAdapter
3. List<Map
<String,?>>: SimpleAdapter
ArrayAdapter
需要把数据放入一个数组以便显示;
android
.R.layout.simple_list_item_1是系统
定义
好的布局文件
只显示一行文字
SimpleCursorAdapter
是和数据库有关的东西,而sqlite数据库所要求的 自动
增长的 id 必须是 _id这中形式
SimpleAdapter
能定义各种各样的布局出来,可以放上ImageView
(图片),还可以放上Button(按钮),CheckBox(复选框)
Arrayadapter代码
public class List1 extends Activity {
private ListVeiw listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListVeiw(this);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
setContentVieww(listView);
}
private String[] mStrings = {
"Abbaye de Belloc"
,
"Abbaye du Mont des Cats"
,
"Acorn"
,
"Adelost"
,
"Affidelice au Chablis"
,
"Aisy Cendre"
,
"Allgauer Emmentaler"
,
"Alverca"
,
"Ami du Chambertin"
,
"Anejo Enchilado"
, "Anneau
"Aragon"
,
"Ardi Gasna"
,
"Ardrahan"
, "Armenian
"Asadero"
,
"Asiago"
,
"Aubisque Pyrenees"
,
"Autun"
,
"Babybel"
,
"Baguette Laonnaise"
,
"Bakers"
,
"Bal"
};
}
Arrayadapter代码
public class List1 extends Activity {
private ListVeiw listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListVeiw(this);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
setContentVieww(listView);
}
private String[] mStrings = {
"Abbaye de Belloc"
,
"Abbaye du Mont des Cats"
,
"Acorn"
,
"Adelost"
,
"Affidelice au Chablis"
,
"Aisy Cendre"
,
"Allgauer Emmentaler"
,
"Alverca"
,
"Ami du Chambertin"
,
"Anejo Enchilado"
, "Anneau
"Aragon"
,
"Ardi Gasna"
,
"Ardrahan"
, "Armenian
"Asadero"
,
"Asiago"
,
"Aubisque Pyrenees"
,
"Autun"
,
"Babybel"
,
"Baguette Laonnaise"
,
"Bakers"
,
"Bal"
};
}
public class List2 extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListView(this);
Cursorc=getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME} ,
new int[] {android.R.id.text1});
listView.setAdapter(adapter);
setContentView(listView);
}
}
public class List2 extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListView(this);
Cursorc=getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME} ,
new int[] {android.R.id.text1});
listView.setAdapter(adapter);
setContentView(listView);
}
}
public class List3 extends ListActivity {
List<Map<String, Object>> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = getListForSimpleAdapter(); //list为数据集合
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.item,
new String[] {
"BigText"
,
"LittleText"
,
"img"
},
new int[] { R.id.BigText,R.id.LittleText, R.id.img });
//可以想成把一个放好数据的布局文件放入本界面中
setListAdapter(adapter);
}
private List<Map<String, Object>> getListForSimpleAdapter() {
List<Map<String,Object>> list=newArrayList<Map<String, Object>>(
3
);
Map<String, Object> map = new HashMap<String, Object>();
map.put(
"BigText"
,
"Android"
);
map.put(
"LittleText"
,
"Google phone."
);
map.put(
"img"
, R.drawable.n);
list.add(map);
map = new HashMap<String, Object>();
map.put(
"BigText"
,
"Lenovo"
);
map.put(
"LittleText"
,
"Ophone"
);
map.put(
"img"
, R.drawable.o);
list.add(map);
map = new HashMap<String, Object>();
map.put(
"BigText"
,
"Droid"
);
map.put(
"LittleText"
,
"Motorola"
);
map.put(
"img"
, R.drawable.droid);
list.add(map);
return list;
}
}