1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import ArrayList from '@ohos.util.ArrayList'; 17import { AddDialog } from '../components/arraylistcomponents/AddDialog'; 18import { ArrayListItem } from '../components/arraylistcomponents/ArrayListItem'; 19import { Information } from '../model/Information'; 20import { logger } from '@ohos/common'; 21import { MyDataSource } from '../components/arraylistcomponents/MyDataSource'; 22 23const TAG: string = 'ArrayListView'; 24 25@Preview 26@Component 27export struct ArrayListView { 28 @State dataSource: Information[] = []; 29 private informations: Information[] = []; 30 private dialogController: CustomDialogController = new CustomDialogController({ 31 builder: AddDialog({ 32 saveData: (writeInformation: Information) => { 33 this.informations.push(writeInformation); 34 this.dataSource = this.informations; 35 logger.info(TAG, `this.list= ${this.dataSource.length}`); 36 } 37 }), 38 autoCancel: true, 39 alignment: DialogAlignment.Default 40 }); 41 42 build() { 43 Column() { 44 Row() { 45 Blank() 46 Button() { 47 Image($r('app.media.ic_add')) 48 .height(35) 49 .width('100%') 50 .objectFit(ImageFit.Contain) 51 .align(Alignment.End) 52 } 53 .key('addBtn') 54 .width(35) 55 .type(ButtonType.Normal) 56 .backgroundColor($r("app.color.button_bg")) 57 .align(Alignment.End) 58 .onClick(() => { 59 this.dialogController.open(); 60 logger.info(TAG, `enter the AddDialog`); 61 }) 62 } 63 .width('100%') 64 65 List({ space: 12 }) { 66 ForEach(this.dataSource, (item: Information, index: number) => { 67 ListItem() { 68 ArrayListItem({ information: item, index: index, handleOnClick: this.handleOnClick }) 69 } 70 }, (item: Information, index: number) => JSON.stringify(item) + index) 71 } 72 .margin({ top: 10 }) 73 } 74 .width('100%') 75 .height('100%') 76 } 77 78 handleOnClick = (index: number) => { 79 this.dataSource = this.informations.splice(index); 80 } 81}