HarmonyOS 鸿蒙Next DataChangeListener.onDatasetChange问题

发布于 1周前 作者 itying888 来自 鸿蒙OS

HarmonyOS 鸿蒙Next DataChangeListener.onDatasetChange问题 点击"从头部填加一个"按钮那里有注释,跟期望不太一样

export class LazyForEachAdapter<T> implements IDataSource {
private listeners: DataChangeListener[] = [];
originDataArray: T[] = [];

public totalCount(): number {
return this.originDataArray.length;
}

public getData(index: number): T {
return this.originDataArray[index];
}

registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
}

unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
// console.info('remove listener');
this.listeners.splice(pos, 1);
}
}

notifyDataReload(): void {
this.listeners.forEach(listener => {
listener.onDataReloaded()
})

}

notifyDataAdd(index: number): void {
this.listeners.forEach(listener => {
listener.onDataAdd(index);

})
}

notifyDataChange(index: number): void {
this.listeners.forEach(listener => {
listener.onDataChange(index);
})
}

notifyDataDelete(index: number): void {
this.listeners.forEach(listener => {
listener.onDataDelete(index);
})
}

notifyDataMove(from: number, to: number): void {
this.listeners.forEach(listener => {
listener.onDataMove(from, to);
})
}

//DataAddOperation | DataDeleteOperation | DataChangeOperation | DataMoveOperation | DataExchangeOperation | DataReloadOperation
/**
* 使用这个api的时候,这个类只能使用onDatasetChange这一种形式,也就是类中只能包含onDatasetChange刷新方式要么就不包含要不就报错
* @param dataOperations
*/
notifyWithDataOperation(dataOperations: DataOperation[]): void {
this.listeners.forEach(listener => {
listener.onDatasetChange(dataOperations)
})

}

notifyDataReloadWithOperation(): void {
this.listeners.forEach(listener => {
listener.onDatasetChange([{} as DataReloadOperation])
})

}

notifyDataRemoveWithOperation(index: number, count?: number): void {
this.listeners.forEach(listener => {
listener.onDatasetChange([{
index: index,
//默认是一个
count: count
} as DataDeleteOperation])
})

}

notifyDataAddWithOperation(index: number, count?: number, key?: string | string[] | undefined): void {
this.listeners.forEach(listener => {
listener.onDatasetChange([{
index: index,
//默认是一个
count: count,
//Key of added data
key: key
} as DataAddOperation])
})

}

notifyDataChangeWithOperation(index: number, key?: string): void {
this.listeners.forEach(listener => {
listener.onDatasetChange([{
index: index,
key: key
} as DataChangeOperation])
})

}

notifyDataMoveWithOperation(from: number, to: number, key?: string): void {
this.listeners.forEach(listener => {
listener.onDatasetChange([{
index: {
from: from,
to: to
},
key: key
} as DataMoveOperation])
})

}

notifyDataExChangeWithOperation(index: ExchangeIndex, key?: ExchangeKey): void {
this.listeners.forEach(listener => {
listener.onDatasetChange([{
index: index,
key: key
} as DataExchangeOperation])
})

}


}



@Entry
@Component
struct swiperIndicatorPage {
private adapter: LazyForEachAdapter<string> = new LazyForEachAdapter()

aboutToAppear(): void {
this.adapter.originDataArray.push(...["测试1", "测试2"])
}

build() {
Column() {
Text("从头部填加一个")
.width("100%")
.height(44)
.onClick(() => {
this.adapter.originDataArray.unshift("头部添加的")
//方式1是有效的
//this.adapter.notifyDataAdd(0)
//方式2是直接添加在底部并且是:"测试2",跟预期的不一样
this.adapter.notifyDataAddWithOperation(0, 1,("头部添加的" + 0))
})
List() {
LazyForEach(this.adapter, (value: string, index) => {
ListItem(){
Text(value)
.width("100%")
.height(44)
}
}, (value: string, index: number) => {
return value + index
})
}
}

}

更多关于HarmonyOS 鸿蒙Next DataChangeListener.onDatasetChange问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

这边需要加上type: DataOperationType.ADD,具体使用可以参考:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5#非首次渲染

export class LazyForEachAdapter implements IDataSource {
  private listeners: DataChangeListener[] = [];
  originDataArray: T[] = [];

  public totalCount(): number {
    return this.originDataArray.length;
  }

  public getData(index: number): T {
    return this.originDataArray[index];
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    if (this.listeners.indexOf(listener) < 0) {
      this.listeners.push(listener);
    }
  }

  unregisterDataChangeListener(listener: DataChangeListener): void {
    const pos = this.listeners.indexOf(listener);
    if (pos >= 0) {
      this.listeners.splice(pos, 1);
    }
  }

  notifyDataReload(): void {
    this.listeners.forEach(listener => {
      listener.onDataReloaded();
    })
  }

  notifyDataAdd(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataAdd(index);
    })
  }

  notifyDataChange(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataChange(index);
    })
  }

  notifyDataDelete(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataDelete(index);
    })
  }

  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach(listener => {
      listener.onDataMove(from, to);
    })
  }

  //DataAddOperation | DataDeleteOperation | DataChangeOperation | DataMoveOperation | DataExchangeOperation | DataReloadOperation

  /**
   使用这个api的时候,这个类只能使用onDatasetChange这一种形式,也就是类中只能包含onDatasetChange刷新方式要么就不包含要不就报错
   @param dataOperations
   */
  notifyWithDataOperation(dataOperations: DataOperation[]): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange(dataOperations)
    })
  }

  notifyDataReloadWithOperation(): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange([{} as DataReloadOperation])
    })
  }

  notifyDataRemoveWithOperation(index: number, count?: number): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange([{
        index: index,
        //默认是一个
        count: count
      } as DataDeleteOperation])
    })
  }

  notifyDataAddWithOperation(index: number, count?: number, key?: string | string[] | undefined): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange([{
        type: DataOperationType.ADD,
        index: index,
      } as DataAddOperation])
    })
  }

  notifyDataChangeWithOperation(index: number, key?: string): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange([{
        index: index,
        key: key
      } as DataChangeOperation])
    })
  }

  notifyDataMoveWithOperation(from: number, to: number, key?: string): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange([{
        index: {
          from: from,
          to: to
        },
        key: key
      } as DataMoveOperation])
    })
  }

  notifyDataExChangeWithOperation(index: ExchangeIndex, key?: ExchangeKey): void {
    this.listeners.forEach(listener => {
      listener.onDatasetChange([{
        index: index,
        key: key
      } as DataExchangeOperation])
    })
  }
}

@Entry
@Component
struct swiperIndicatorPage {
  private adapter: LazyForEachAdapter = new LazyForEachAdapter()

  aboutToAppear(): void {
    this.adapter.originDataArray.push(...["测试1", "测试2"])
  }

  build() {
    Column() {
      Text("从头部填加一个")
        .width("100%")
        .height(44)
        .onClick(() => {
          this.adapter.originDataArray.unshift("头部添加的")
          //方式1是有效的
          //this.adapter.notifyDataAdd(0)
          //方式2是直接添加在底部并且是:"测试2",跟预期的不一样
          // this.adapter.originDataArray.splice(0, 0, "头部添加的" + 0);
          this.adapter.notifyDataAddWithOperation(0)
        })
      List() {
        LazyForEach(this.adapter, (value: string, index) => {
          ListItem() {
            Text(value)
              .width("100%")
              .height(44)
          }
        }, (value: string, index: number) => {
          return value + index
        })
      }
    }
  }
}

更多关于HarmonyOS 鸿蒙Next DataChangeListener.onDatasetChange问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对帖子标题中提到的HarmonyOS(鸿蒙)Next DataChangeListener.onDatasetChange问题,以下是根据要求提供的简洁回答:

在HarmonyOS中,DataChangeListener接口用于监听数据集的变化。onDatasetChange方法是该接口的核心方法,当数据集发生变化时会被触发。如果你遇到了onDatasetChange方法不工作或者表现异常的问题,可能的原因包括但不限于:

  1. 监听器未正确注册:确保你的DataChangeListener实例已经被正确注册到相应的数据源或组件上。

  2. 数据源未更新:检查触发onDatasetChange的数据源是否确实发生了更新。有时候数据源更新逻辑可能有误,导致监听器无法被触发。

  3. 线程问题:如果数据更新和监听器回调在不同的线程上执行,可能会遇到线程同步问题。确保数据更新和监听器回调在正确的线程上执行。

  4. 权限问题:某些数据集可能需要特定的权限才能访问或监听其变化,确保你的应用已经获得了这些权限。

  5. 版本兼容性:检查你的HarmonyOS SDK版本是否与你的开发环境兼容,有时候版本更新会带来API的变更。

如果以上检查都无法解决问题,可能是更深层次的系统或框架问题。此时,建议直接联系官网客服以获取更专业的帮助。官网地址是:

https://www.itying.com/category-93-b0.html

回到顶部