文章摘要:
1瘾境、從設(shè)計(jì)模式的角度淺談List列表應(yīng)用程序開(kāi)發(fā)。
2镰惦、列表應(yīng)用程序開(kāi)發(fā)三要素迷守。控件陨献、數(shù)據(jù)盒犹、適配器。
一眨业、綜述:
1急膀、Android中,使用ListView配合Adapter來(lái)展示數(shù)據(jù)列表的例子隨處可見(jiàn)龄捡。但在實(shí)際應(yīng)用場(chǎng)景中卓嫂,數(shù)據(jù)源類型、View展示方式等千差萬(wàn)別聘殖。
2晨雳、策略模式:將公共的部分抽離出來(lái)行瑞,使用策略專注應(yīng)對(duì)變化。
3餐禁、列表應(yīng)用程序中最主要的變化是數(shù)據(jù)源的格式類型血久,既可以是數(shù)組,又可以是List集合帮非、Cursor等氧吐,故而:原生Android提供了ListAdapter、ArrayAdapter末盔、CursorAdapter等策略來(lái)應(yīng)對(duì)變化筑舅。
二、列表程序三要素陨舱。
1翠拣、顯示列表控件。
在Android中游盲,ListView用來(lái)展示列表應(yīng)用程序误墓。ListView控件有多種方式可以獲得,android提供了ListActivity背桐,可以直接通過(guò)getListView()獲得优烧。
public class ViewVibration extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
}
}
2链峭、列表展示數(shù)據(jù)畦娄。
本例子演示ArrayAdapter適配器與ListView配合展示列表程序,故而:數(shù)據(jù)源類型為Array弊仪。
public static final String[] TITLES =
{
"Henry IV (1)",
"Henry V",
"Henry VIII",
"Richard II",
"Richard III",
"Merchant of Venice",
"Othello",
"King Lear"
};
3熙卡、適配器。
getListView().setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, TITLES));
通過(guò)如上三步励饵,列表應(yīng)用程序驳癌,就可以正常展示了。
代碼下載:
git clone git@github.com:drodYoung/DrodYoungTestDemo.git
運(yùn)行圖片: