在HarmonyOS NEXT开发中ListItemGroup中ListItem数据有更新,如何刷新List?

在HarmonyOS NEXT开发中ListItemGroup中ListItem数据有更新,如何刷新List?

阅读 1.1k
1 个回答

将Group中的数据抽象成一个组件,在组件中传递Group所需的参数,代码:

@Observed 
class ProjectArray extends Array<string>{ 
} 
 
@Observed 
class Subject { 
  public title: string; 
  public projects: ProjectArray; 
  constructor(title, projects) { 
    this.title = title; 
    this.projects = projects; 
  } 
} 
 
@Component 
struct ViewA { 
  title: string; 
  @ObjectLink projects: ProjectArray; 
  build() { 
    Column() { 
      Text(this.title) 
      List(){ 
        ForEach(this.projects, (project) => { 
          ListItem() { 
            Text(project) 
              .width("100%").height(100).fontSize(20) 
              .textAlign(TextAlign.Center).backgroundColor(0xFFFFFF) 
          } 
 
        }) 
      } 
    } 
  } 
} 
 
@Entry 
@Component 
struct ListItemGroupExample2 { 
  @State timetable: Subject[] = [ 
    new Subject('星期一', new ProjectArray('语文', '数学', '英语')) 
  ] 
  build() { 
    Column() { 
      Button('change') 
        .onClick(() => { 
          console.log('testTag: Change'); 
          this.timetable[0].projects.splice(0, 1, '英语'); 
        }) 
      ForEach(this.timetable, (table) => { 
        ViewA({title: table.title, projects: table.projects}) 
      }) 
 
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 20 }) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进