
学习目标:
开发一个简单的记录消费行为的应用,以列表的形式展现消费记录,当没有记录时,显示提示问题:No content!。
学习目的:
了解LinearLayout、ListView和TextView,掌握ListActivity的基本应用,掌握如何绑定数据。
学习收获:
为了能够实现在有数据时,呈现数据列表,没有数据时,呈现文字提示信息的效果,我们要修改ListActivity的默认布局。通过在onCreate方法里,调用setContentView即可达到自定义布局的效果。
如以下(notes_list.xml)所示:
在上面文字中,list和empty是android平台提供的,所以必须通过用android:id来做前缀。同时也了解到LinearLayout是一种GroupView,能够水平或者垂直摆放View元素。
ListActivity就是通过绑定一个数据源,例如数组或者Cursor,来展现条目列表,并且暴露事件(用户选中某个条目)的handler。而把ListActivity的ListView对象和数据绑定起来正是通过一个类,这个类要实现ListAdapter接口。
Android提供了两种标准的list adapter:适用于静态数据(Maps)的SimpleAdapter,和适用于查询结果光标的SimpleCursorAdapter。
public class Purse extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_list);
// The Maps contain the data for each row, and should include all the entries specified in "from"
Map tv = new HashMap();
tv.put("type","TV");
tv.put("money", "200.0");
Map phone = new HashMap();
phone.put("type", "PHONE");
phone.put("money", "50.0");
Map notebook = new HashMap();
notebook.put("type", "NOTEBOOK");
notebook.put("money", "500.0");
// A List of Maps. Each entry in the List corresponds to one row in the list.
List
有了整个屏幕的布局以后,还可以单独指定列表中行的布局。要做到这一点是通过activity持有的ListAdapter对象。
行布局信息如以下内容(notes_row.xml)所示: