PALM开发教程-第九章 分类和查找 |
| 作者:palmheart 来源:palmheart.net 发布时间:2005-12-21 3:36:55 |
|
H.9 Clear the category bits recAttrs &= ~dmRecAttrCategoryMask; // CH.9 Set the category to the appropriate category if( listCat == dmAllCategories ) recAttrs |= dmUnfiledCategory; else recAttrs |= listCat; // CH.9 Set the record attributes DmSetRecordInfo( contactsDB, cursor, &recAttrs, NULL ); 在attachFields()中,我们需要设置弹出触发按纽的标签文本。首先,在函数的顶部再定义一些新的变量。 UInt recAttrs; // CH.9 The record attribute bits Char catName[dmCategoryLength]; // CH.6 The category name 在函数的末尾,我们使用一些专门的分类管理函数设置并重新得到标签文本。 // CH.9 Get the record attributes DmRecordInfo( contactsDB, cursor, &recAttrs, NULL, NULL ); // CH.9 Get the category detailCat = recAttrs & dmRecAttrCategoryMask; // CH.9 Set the category popup trigger label CategoryGetName( contactsDB, detailCat, catName ); CategorySetTriggerLabel( getObject( form, ContactDetailCategoryPopupPopTrigger ), catName ); 这样,使分类能在Contact List窗体上正常工作的代码修改工作就完成了,其中包括将具体的记录写入分类中。 对Contact List Form的支持 对Contact List窗体的修改要更多一些,因为我们需要将记录仅仅显示在一个给出的分类或者All分类中。问题是先前的代码很大程度上依赖于数据库中的每一个记录,例如,我们可以通过Cursor+1实现显示下一条记录。但是在分类中,我们就不能这样做了,因为里面或许只有一些甚至没有分类中所要显示的记录。 要知道当前分类到底有没有这条记录的一种方法是将记录用函数包装起来,这样我们就能象以前那样来浏览记录了。在程序的开头,先声明三个新函数的原型: static void initIndexes( void ); static void scrollIndexes( Int amount ); static UInt findIndex( UInt scrollValue ); 函数initIndexes()从当前分类中获得与之相关联的表的索引tableIndex。这样,我们就可以在指定的表中从头到尾的浏览,从而取代盲目地在所有记录间浏览。函数scrollIndexes()允许我们把具有特殊分类的窗口上移或下移。函数findIndex()返回给我们一个记录在分类中的游标位置。在以后的部分中将看到这些函数是如何工作的,现在我们只是在Contact List 窗体的事件处理函数及其它函数中调用它们来实现浏览的功能。 在Contact List窗体事件处理函数 contactListHandleEvent()应该怎样做呢?首先,在函数的开头保存分类的名称。 Char catName[dmCategoryLength]; // CH.9 Category name 修改窗体打开事件,设置弹出触发按纽的标签文本,并调用initIndexes()来初始化列表框。 // CH.7 Form open event case frmOpenEvent: { // CH.7 Draw the form FrmDrawForm( form ); // CH.9 Set the category popup trigger label CategoryGetName( contactsDB, listCat, catName ); CategorySetTriggerLabel( getObject( form, ContactListCategoryPopupPopTrigger ), catName ); // CH.8 The cursor starts at the beginning cursor = 0; // CH.9 Initialize the table indexes initIndexes(); // CH.8 Populate and draw the table drawTable(); } break; 当选中一个记录切换到Contact Detail窗体时,我们在tableIndex数组中查找所选中记录的游标位置。 // CH.7 Respond to a list selection case tblSelectEvent: { // CH.7 Set the database cursor to the selected contact cursor = tableIndex[event->data.tblSelect.row]; // CH.7 Go to contact details FrmGotoForm( ContactDetailForm ); } break; 在popSelectEvent事件中,由于把记录依据不同的标准进行了重新排序,所以必须根据排序的结果重新初始化tableIndex。 // CH.7 Sort the contact database by the new criteria DmQuickSort( contactsDB, (DmComparF*)sortFunc, sortBy ); // CH.8 Cursor starts at zero cursor = 0; // CH.9 Initialize the table indexes initIndexes(); // CH.8 Rebuild the table drawTable(); 我们调用scrollIndexes()代替向上、向下箭头重复按钮所做的数学运算。 // CH.8 Up arrow case ContactListRecordUpRepeating: scrollIndexes( -1 ); break; // CH.8 Down arrow case ContactListRecordDownRepeating: scrollIndexes( 1 ); break; 同样的,我们通过调用scrollIndexes()代替用按键事件的游标运算。 // CH.8 Up arrow hard key case pageUpChr: scrollIndexes( -(TABLE_NUM_ROWS - 1) ); break; // CH.8 Down arrow hard key case pageDownChr: scrollIndexes( TABLE_NUM_ROWS - 1 ); break; 为了处理滚动条,我们需要做两件事。首先,由于刷新基于滚动条的记录的时间变长了,那么我们通过响应sclExitEvent时间来代替响应sclRepeatEvent。第二,使用findIndex()在分类记录的子集中找到任意记录的游标值。我们必须仅仅处理基于当前分类的记录的滚动条。如果把表滚动到底的话,返回的数字是处在0和表顶端的记录之间。我们用findIndex()把这个数字转化为一个绝对的游标值。一旦知道表顶部记录的实际位置,我们调用initIndexes()把余下的值赋给tableIndex数组。 // CH.8 Respond to scrollbar events case sclExitEvent: { //CH.9 Find the record in our category cursor = findIndex( event->data.sclExit.newValue ); // CH.9 Initialize our index list initIndexes(); // CH.8 Draw the table drawTable(); } break; 下面是处理分类弹出触发按纽的代码。和Contact Detail窗体相比,我们为触发器捕捉ctlSelectEvent事件,并把控件传递给CategorySelect(),为了防止采用弹出触发器的缺省操作,函数向Palm OS返回True。请注意我们在CategorySelect()中使用了True参数,它将该在分类列表框中添加All选项。一旦新的分类被选中,我们就需要重新做索引并重新绘制表。 // CH.9 Catch a tap on the category trigger case ctlSelectEvent: { // CH.9 Palm OS will present the popup list for us. CategorySelect( contactsDB, form, ContactListCategoryPopupPopTrig |
| [] [返回上一页] [打 印] |
|
文章评论 |
