[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
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
@Override
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);
view.setTag(holder);
}
if (cursor.getInt(holder.colImp) > 0) {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.orange));
} else {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
}
}
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.
<翻译>
我们用适配器把所有提醒登记到ListView。在运行时中,ListView将重复调用在适配器里的bindView()方法,以单独的示图对象作为用户装载器,且在清单里滚动。填入这些清单条目到示图里是适配器的工作。在这个例子代码里,我们使用了适配器的子类SimpleCursorAdapter。这个类用了一个游标对象,它保存着跟踪表单里的行轨迹。

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.
<翻译>
这里你看到了一个ViewHolder模式的例子。这是一个容易认识的Android模式,每个ViewHolder对象含有一个示图的标签。用数据源(在这个例子里是Cursor)的值,这个对象加载清单里的示图对象。ViewHolder定义为一个内部静态类,有两个实例变量,一个用于索引重要的表单项另一个用于在布局里定义的row_tab示图。

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.
<翻译>
早先你用了ArrayAdapter来管理模型和示图之间的关系。SimpleCursorAdapter也是用同样的模式,虽然它的模型是SQLite数据库。将清单5-14更改到你的RemindersDbAdaper和RemindersSimpleCursorAdapter类里。
Listing 5-14. RemindersActivity Code
public class RemindersActivity extends ActionBarActivity {
private ListView mListView;
private RemindersDbAdapter mDbAdapter;
private RemindersSimpleCursorAdapter mCursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
mListView = (ListView) findViewById(R.id.reminders_list_view);
mListView.setDivider(null);
mDbAdapter = new RemindersDbAdapter(this);
mDbAdapter.open();
Cursor cursor = mDbAdapter.fetchAllReminders();
//from columns defined in the db
String[] from = new String[]{
RemindersDbAdapter.COL_CONTENT
};
//to the ids of views in the layout
int[] to = new int[]{
R.id.row_text
};
mCursorAdapter = new RemindersSimpleCursorAdapter(
//context
RemindersActivity.this,
//the layout of the row
R.layout.reminders_row,
//cursor
cursor,
//from columns defined in the db
from,
//to the ids of views in the layout
to,
//flag - not used
0);
// the cursorAdapter (controller) is now updating the listView (view)
//with data from the db (model)
mListView.setAdapter(mCursorAdapter);
}
//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把例子里的条目加回来。这将在下章描述,你可以继续看下去并检查下作业了。

Summary

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.
<翻译>
小结
致此,你有了个成熟的Android应用。在这章,你学会了怎样设置第一个Android项目并用Git控制代码。你也探索了怎么编辑Android布局,用设计或文本模式。你也看到建立一个在动作条里的溢出菜单。这章的最后探索了ListView和Adapter,以及绑定数据到内建的SQLite数据库。在接下来的章节里,增加创建和编辑提醒的功能,你将完成这个app。
2016-03-26 09:33 添加评论 分享
已邀请:

要回复问题请先登录注册

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