1/* 2* Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 PlainArray from '@ohos.util.PlainArray'; 17import ArrayList from '@ohos.util.ArrayList'; 18import { BaseDataSource } from '@ohos/common/src/main/ets/components/BaseDataSource'; 19import { KeyValuePair } from '../../model/KeyValuePair'; 20 21export class PlainArrayDataSource extends BaseDataSource { 22 private dataArr: PlainArray<string> = new PlainArray(); 23 private arr: ArrayList<number> = new ArrayList(); 24 25 public totalCount(): number { 26 return this.arr.length; 27 } 28 29 public getData(index: number): KeyValuePair { 30 let key: number = this.arr[index]; 31 let value: string = this.dataArr.get(key); 32 this.dataArr.get(this.arr[index]); 33 let keyValuePair: KeyValuePair = new KeyValuePair(`${this.arr[index]}`, value); 34 return keyValuePair; 35 } 36 37 public addData(keyValuePair: KeyValuePair): void { 38 let key: number = Number(keyValuePair.key); 39 if (!this.dataArr.has(key)) { 40 this.arr.add(key) 41 } 42 this.dataArr.add(key, keyValuePair.value); 43 this.notifyDataAdd(this.dataArr.length - 1); 44 this.notifyDataReload(); 45 } 46 47 public deleteData(index: number): void { 48 this.dataArr.removeAt(index); 49 this.arr.removeByIndex(index); 50 this.notifyDataDelete(index); 51 this.notifyDataReload(); 52 } 53}