优化运动记录list
authorCaisking <Casiking@zhihuishixun.com>
Tue, 3 Feb 2026 10:49:49 +0000 (18:49 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Tue, 3 Feb 2026 10:49:49 +0000 (18:49 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/traingrecord/include/TraingRecordItemView.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/traingrecord/src/TraingRecordPresenter.cpp

index 93d2bb3..0fd50ec 100644 (file)
@@ -48,7 +48,7 @@ namespace OHOS
         UILabelExt sLabeldata;   // 标签
         UILabelExt sLabeltime;   // 标签
         UIImageView slogo;       // 图标
-        TraingRecordItem sItemInfo = {0, nullptr, nullptr, nullptr};
+        TraingRecordItem sItemInfo = {-1, "", "", ""};  // index=-1 表示未设置,避免误用导致越界
     };
 
 }
index 9d3fd46..9e18454 100644 (file)
@@ -124,10 +124,13 @@ namespace OHOS
         }
         else
         {
-            TraingRecordItemView *p_TraingRecordItemView = static_cast<TraingRecordItemView *>(&view);
+            TraingRecordItemView *p_TraingRecordItemView = dynamic_cast<TraingRecordItemView *>(&view);
+            if (p_TraingRecordItemView == nullptr)
+            {
+                return false;
+            }
             int index = p_TraingRecordItemView->GetTraingRecordIndex();
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the index is %d", index);
-            // 读取json数据
             std::vector<SportData> sportDataVector;
             if (!TraingModel::GetInstance()->LoadDataFormJson(sportDataVector))
             {
@@ -136,8 +139,12 @@ namespace OHOS
             }
             if (!sportDataVector.empty())
             {
-                // 选择点击按键的历史数据
-                int arraynum = sportDataVector.size() - 1;
+                int arraynum = static_cast<int>(sportDataVector.size()) - 1;
+                if (index < 0 || index > arraynum)
+                {
+                    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "invalid record index %d, size=%zu", index, sportDataVector.size());
+                    return false;
+                }
                 SportData sportData = sportDataVector[arraynum - index]; // 显示是倒序 id同理
                 // 先设置运动数据,确保sportId_被正确更新,然后再进入界面
                 // 这样OnStart()中调用IsNeedGPS()时能正确判断