Android学习过程中遇到的问题与分析

把Android学习过程中遇到的问题以及自己分析感悟记录下来,对Android学习算是一个梳理。不断更新新问题。随机整理,想到什么问题就记下来。
1.Activity和View是什么关系?
Activity是Android最常用的一个组成模块,通常一个Activity就是展示一屏信息,换了另一个界面,就是换了一个Activity。但是View不也是在屏幕上显示信息吗,有什么关系啊。实际上View更加单纯,它只是显示UI,如可以只是一个button view,或者只显示text (text view)。而Activity则要复杂的多,它除了要显示一屏信息,还要响应用户事件。如用户按了menu键,我们必须在Activity中告诉系统如何响应这个menu按键,显示哪些menu等等。另外View都是在XML文件中定义,而在Activity中被调用。只是个人理解,欢迎指正补充。
2.Notepadv1中fillData()解析。
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);

String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };

// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
首先看下什么是Cursor?
Google是这样解释的:
This interface provides random read-write access to the result set returned by a database query.
也就是说它是一个接口,该接口可以对数据库查询结果进行随机读写访问。实际上这是Android系统为了避免返回大量rows,而采取的一种提高效率的方法。我们通常在Android平台上都是用Cursor对象来接收一个数据库查询结果。并通过Cursor对象对其结果进行各种操作或作为参数传递给其它method.
那么startManagingCursor(c);又是干什么用的呢?
先看google的解释:
we use an Activity method called startManagingCursor() that allows Android to take care of the Cursor lifecycle instead of us needing to worry about it. (We will cover the implications of the lifecycle in exercise 3, but for now just know that this allows Android to do some of our resource management work for us.)
翻译过来就是让Android去关注并管理该Cursor的生命周期,而不需要我们去操心。更多解释稍后再议 :D 。
代码中构造了一个SimpleCursorAdapter对象,这里使用该类,其用途是为了绑定Cursor和相关的layout中域,说白了就是告诉系统我要显示哪些列的数据,并且针对这些列,应该按照什么格式去显示.该构造函数原型如下:
public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
该对象的参数就是我们要绑定的东西。第一个参数是context,通常传this。第二个是我们要绑定的View的resource ID(这里是R.layout.notes_row)。第三个参数是Cursor。第四个参数是我们要显示的Column String Array,这就是之前我们通过
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
来新建了一个数组,该数组中只有一个String就是title String。第五个参数先看google的解释:
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"指定的column返回的数据按照"to"指定的格式进行显示。这里的"to"只有一个格式就是R.id.text1。(注意text1是在layout: notesRow.xml中定义的)。这里需要注意的是如果from中有多个String,那么To中就要有多个格式相对应,并且是按照顺序。
最后通过setListAdapter(notes);来显示该List。这个method中参数类型是ListAdapter,而实际上SimpleCursorAdapter是ListAdapter的一个实际class。ListAdapter只是一个接口。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值