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/src/main/ets/util/Logger'; 21 22const TAG: string = 'ArrayListView'; 23 24@Preview 25@Component 26export struct ArrayListView { 27 @State dataSource: Information[] = []; 28 private informations: ArrayList<Information> = new ArrayList(); 29 private dialogController: CustomDialogController = new CustomDialogController({ 30 builder: AddDialog({ 31 saveData: (writeInformation: Information) => { 32 this.informations.add(writeInformation); 33 this.dataSource = this.informations.convertToArray(); 34 logger.info(TAG, `this.list= ${this.dataSource.length}`); 35 } 36 }), 37 autoCancel: true, 38 alignment: DialogAlignment.Default 39 }); 40 41 build() { 42 Column() { 43 Row() { 44 Blank() 45 Button() { 46 Image($r('app.media.ic_add')) 47 .height(35) 48 .width('100%') 49 .objectFit(ImageFit.Contain) 50 .align(Alignment.End) 51 } 52 .key('addBtn') 53 .width(35) 54 .type(ButtonType.Normal) 55 .backgroundColor($r("app.color.button_bg")) 56 .align(Alignment.End) 57 .onClick(() => { 58 this.dialogController.open(); 59 logger.info(TAG, `enter the AddDialog`); 60 }) 61 } 62 .width('100%') 63 64 List({ space: 12 }) { 65 ForEach(this.dataSource, (item: Information, index: number) => { 66 ListItem() { 67 ArrayListItem({ information: item, index: index, handleOnClick: this.handleOnClick }) 68 } 69 }, (item: Information, index: number) => JSON.stringify(item) + index) 70 } 71 .margin({ top: 10 }) 72 } 73 .width('100%') 74 .height('100%') 75 } 76 77 handleOnClick = (index: number) => { 78 this.informations.removeByIndex(index); 79 this.dataSource = this.informations.convertToArray(); 80 } 81}