[Learn Android Studio 汉化教程]第五章:Reminders实验:第一部分(续)

Last, there are two delete methods. The first takes an id parameter and uses the database object to generate and execute a delete statement for a particular reminder. The second method requests that the database generate and execute a delete statement to remove all the reminders from the table.
最后,有两个删除方法。第一个针对特定的提醒用 id参数和数据库对象来生成和执行一条删除语句。第二个方法需要数据库生成并执行一条删除语句来移除所有表单里的提醒。

At this point, you need a means of getting reminders out of the database and into the ListView. Listing 5-13 demonstrates the logic necessary to bind database values to individual row objects by extending the special Adapter Android class you saw earlier. Create a new class called RemindersSimpleCursorAdapter in the com.apress.gerber.reminders package and decorate it with the proceeding code. As you resolve imports, use
the android.support.v4.widget.SimpleCursorAdapter class.
这时,你需要一个手段从数据库提出提醒并放入到ListView。清单5-13演示了必要的逻辑,通过继承之前你看到的特别的Adapter Android类来绑定数据库值到单独的行对象。创建一个新类RemindersSimpleCursorAdapter在com.apress.gerber.reminders包下,并完成处理代码。当解析导入类时,使用android.support.v4.widget.SimpleCursorAdapter class类。
Listing 5-13. RemindersSimpleCursorAdapter Code
public class RemindersSimpleCursorAdapter extends SimpleCursorAdapter {
public RemindersSimpleCursorAdapter(Context context, int layout, Cursor c, String[]
from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
//to use a viewholder, you must override the following two methods and define a ViewHolder class
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.colImp = cursor.getColumnIndexOrThrow(RemindersDbAdapter.COL_IMPORTANT);
holder.listTab = view.findViewById(R.id.row_tab);
if (cursor.getInt(holder.colImp) > 0) {
} else {
static class ViewHolder {
//store the column index
int colImp;
//store the view
View listTab;

We register the Adapter with the ListView to populate reminders. During runtime, the ListView will repeatedly invoke the bindView() method on the Adapter with individual onscreen View objects as the user loads and scrolls through the list. It is the job of the Adapter to fill these views with list items. In this code example, we’re using a subclass of Adapter called SimpleCursorAdapter. This class uses a Cursor object, which keeps track of
the rows in the table.

Here you see an example of the ViewHolder pattern. This is a well-known Android pattern in which a small ViewHolder object is attached as a tag on each view. This object adds decoration for View objects in the list by using values from the data source, which in this example is the Cursor. The ViewHolder is defined as a static inner class with two instance variables, one for the index of the Important table column and one for the row_tab view you defined in the layout.

The bindView() method starts by calling the superclass method that maps values from the cursor to elements in the View. It then checks to see whether a holder has been attached to the tag and creates a new holder if necessary. The bindView() method then configures the holder’s instance variables by using both the Important column index and the row_tab you defined earlier. After the holder is either found or configured, it uses the value of the COL_IMPORTANT constant from the current reminder to decide which color to use for the row_tab. The example uses a new orange color, which you need to add to your colors.xml: <color name="orange">#ffff381a</color>.
bindView()方法开始于父类在示图里从游标到元素的map值方法的调用。然后检查看(holder)是否附有一个标签还是有必要创建一个新的(holder)。然后bindView()方法用重要列索引和早先定义的row_tab配置容器的实例变量。在容器被发现或配置后,从当前提醒的COL_IMPORTANT常量秋决定row_tab用什么颜色。这个例子用了新的橙色,那个你要加到colors.xml: <color name="orange">#ffff381a</color>。

Earlier you used an ArrayAdapter to manage the relationship between model and view. The SimpleCursorAdapter follows the same pattern, though its model is an SQLite database. Make the changes in Listing 5-14 to use your new RemindersDbAdapter and RemindersSimpleCursorAdapter.
Listing 5-14. RemindersActivity Code
public class RemindersActivity extends ActionBarActivity {
private ListView mListView;
private RemindersDbAdapter mDbAdapter;
private RemindersSimpleCursorAdapter mCursorAdapter;
protected void onCreate(Bundle savedInstanceState) {
mListView = (ListView) findViewById(R.id.reminders_list_view);
mDbAdapter = new RemindersDbAdapter(this);
Cursor cursor = mDbAdapter.fetchAllReminders();
//from columns defined in the db
String[] from = new String[]{
//to the ids of views in the layout
int[] to = new int[]{
mCursorAdapter = new RemindersSimpleCursorAdapter(
//the layout of the row
//from columns defined in the db
//to the ids of views in the layout
//flag - not used
// the cursorAdapter (controller) is now updating the listView (view)
//with data from the db (model)
//Abbreviated for brevity

If you run the app at this point, you will not see anything in the list; the screen will be completely empty because your last change inserted the SQLite functionality in place of the example data. Press Ctrl+K | Cmd+K and commit your changes with the message Adds SQLite database persistence for reminders and a new color for important reminders. As a challenge, you might try to figure out how to add the example items back by using the new RemindersDbAdapter. This is covered in the next chapter, so you can look ahead and check your work.
这时候如果你运行app,你还是看不到清单里有任何东西;屏幕完全是空的,因为你最后的修改在例子的数据部分插入了数据库功能。按Ctrl+K | Cmd+K并提交的修改,提交信息:Adds SQLite database persistence for reminders and a new color for important reminders。聪明点的话,你可能想弄清楚怎么用新的RemindersDbAdaper把例子里的条目加回来。这将在下章描述,你可以继续看下去并检查下作业了。


At this point, you have a maturing Android app. In this chapter, you learned how to set-up your first Android project and controlled its source using Git. You also explored how to edit Android layouts in both Design and Text mode. You have seen a demonstration of creating an overflow menu in the Action Bar. The chapter concluded by exploring ListViews and Adapters, and binding data to the built-in SQLite database. In the following chapter, you will complete the app by adding the ability to create and edit reminders.
2016-03-26 09:33 添加评论 分享


退出全屏模式 全屏模式 回复