如何解决读取 ble 值并更新 livedata/recyclerview 列表
我有一个 Android 应用程序,可以从设备读取蓝牙值。我在 recyclerView 中呈现这些值。我想找到一种方法来更新此列表,但由于尝试了在 google 上找到的不同解决方案,因此我感到有点迷茫。
我真的不知道是我的 recyclerView 出了问题,还是我的 livedata Observer 没有被触发。当我再次尝试读取值时,列表消失了,但我可以在日志中看到来自视图模型的新值列表
应用在第一次迭代时按照我的预期运行。
如果需要,我可以提供更多代码
在这里,我读取了我检索 byteArray 的值。
ReadLineNodeValues().iRObjectTemperature -> {
bleListviewmodel.addBletoList(characteristic.value)
}
class BleValueviewmodel: viewmodel() {
fun addBletoList(bleValue: ByteArray) {
bluetoothLEvalue.add(bleValue)
mutablelivedataBluetooth.postValue(bluetoothLEvalue)
}
fun getList(): mutablelivedata<ArrayList<ByteArray>> {
return mutablelivedataBluetooth
}
在这里我试图检索我将它添加到我的 recyclverView 的列表
private fun showItems() {
val bleValueviewmodel = viewmodelProvider(requireActivity()).get(BleValueviewmodel::class.java)
bleValueviewmodel.getList().observe(viewLifecycleOwner) {
if (it.size == 26) {
showlist(it)
}
}
}
RecyclerView
class LineNodeValueDataAdapter :
RecyclerView.Adapter<LineNodeBigViewHolder>() {
private val differCallback = object : DiffUtil.ItemCallback<LinenNodeValueData>() {
override fun areItemsTheSame(
oldItem: LinenNodeValueData,newItem: LinenNodeValueData
): Boolean {
return oldItem.valueOne == newItem.valueOne && oldItem.valueTwo == newItem.valueTwo && oldItem.valueThree == newItem.valueThree && oldItem.valueFour == newItem.valueFour
}
override fun areContentsTheSame(
oldItem: LinenNodeValueData,newItem: LinenNodeValueData
): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this,differCallback)
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): LineNodeBigViewHolder {
val binding = CardviewListlayoutValueBinding
.inflate(LayoutInflater.from(parent.context),parent,false)
return LineNodeBigViewHolder(binding)
}
override fun onBindViewHolder(holder: LineNodeBigViewHolder,position: Int) {
val place = differ.currentList[position]
holder.bind(place)
}
override fun getItemCount(): Int = differ.currentList.size
}
我转换这些值,因为它们都是 ByteArrays
fun showlist(listValues: MutableList<ByteArray>) {
//Systemnode
val systemNodeMCUTemp = String(listValues[0])
val systemNodeVoltages = String(listValues[1])
val systemNodeWeatherSensor = String(listValues[2])
val systemNodeOpenThreadCfg = String(listValues[3])
//Linenode
val currentLineNode = String(listValues[4])
val acceleroMeterX = ByteBuffer.wrap(listValues[5]).order(ByteOrder.LITTLE_ENDIAN).float
val acceleroMeterY = ByteBuffer.wrap(listValues[6]).order(ByteOrder.LITTLE_ENDIAN).float
val acceleroMeterZ = ByteBuffer.wrap(listValues[7]).order(ByteOrder.LITTLE_ENDIAN).float
val iRObjectTemperature =
ByteBuffer.wrap(listValues[8]).order(ByteOrder.LITTLE_ENDIAN).float
val contactSensorTemperature =
ByteBuffer.wrap(listValues[9]).order(ByteOrder.LITTLE_ENDIAN).float
val magneticField = ByteBuffer.wrap(listValues[10]).order(ByteOrder.LITTLE_ENDIAN).float
val internalVoltage1V8 =
ByteBuffer.wrap(listValues[11]).order(ByteOrder.LITTLE_ENDIAN).float
val internalVoltagevBusRail =
ByteBuffer.wrap(listValues[12]).order(ByteOrder.LITTLE_ENDIAN).float
val internalVoltageVDD =
ByteBuffer.wrap(listValues[13]).order(ByteOrder.LITTLE_ENDIAN).float
val internalVoltageVDDH =
ByteBuffer.wrap(listValues[14]).order(ByteOrder.LITTLE_ENDIAN).float
val powerHarvestingVoltageSol1 =
ByteBuffer.wrap(listValues[15]).order(ByteOrder.LITTLE_ENDIAN).float
val powerHarvestingVoltageSol2 =
ByteBuffer.wrap(listValues[16]).order(ByteOrder.LITTLE_ENDIAN).float
val powerHarvestingVoltageEmppt =
ByteBuffer.wrap(listValues[17]).order(ByteOrder.LITTLE_ENDIAN).float
val powerHarvestingVoltageHmppt =
ByteBuffer.wrap(listValues[18]).order(ByteOrder.LITTLE_ENDIAN).float
val eHFieldHField =
ByteBuffer.wrap(listValues[19]).order(ByteOrder.LITTLE_ENDIAN).float
val eHFieldEField =
ByteBuffer.wrap(listValues[20]).order(ByteOrder.LITTLE_ENDIAN).float
val ambientTemperaturesAcc =
ByteBuffer.wrap(listValues[21]).order(ByteOrder.LITTLE_ENDIAN).float
val ambientTemperaturesIR =
ByteBuffer.wrap(listValues[22]).order(ByteOrder.LITTLE_ENDIAN).float
val ambientTemperaturesMag =
ByteBuffer.wrap(listValues[23]).order(ByteOrder.LITTLE_ENDIAN).float
val ambientTemperaturesMCU =
ByteBuffer.wrap(listValues[24]).order(ByteOrder.LITTLE_ENDIAN).float
val openThreadCFG =
ByteBuffer.wrap(listValues[25]).order(ByteOrder.LITTLE_ENDIAN).float
val systemNodeList = ArrayList<SystemValue>()
systemNodeList.add(SystemValue("Systemode MguTemp",systemNodeMCUTemp))
systemNodeList.add(SystemValue("Systemnode Voltages",systemNodeVoltages))
systemNodeList.add(SystemValue("Systemnode WeatherSensor",systemNodeWeatherSensor))
systemNodeList.add(SystemValue("Systemnode OpenThreadCfg",systemNodeOpenThreadCfg))
val lineNodevaluelist = ArrayList<LinenNodeValueData>()
val lineNodeListBle = ArrayList<SystemValue>()
lineNodeListBle.add(SystemValue("LineNode Service",currentLineNode))
lineNodeListBle.add(SystemValue("Current LineNode",acceleroMeterX.toString()))
//lineNodeListBle.add(SystemValue("Acclero meter",lineNodeValueSeven.toString()))
Timber.i("Acclerometer2 :: ${acceleroMeterY}}")
lineNodeListBle.add(
SystemValue(
"Accelero Meter","X: $acceleroMeterX " + "Y: $acceleroMeterY"
+ " Z: $acceleroMeterZ"
)
)
lineNodeListBle.add(
SystemValue(
"iRObject Temperature",iRObjectTemperature.toString()
)
)
lineNodeListBle.add(
SystemValue(
"ContactSensor Temperature",contactSensorTemperature.toString()
)
)
lineNodeListBle.add(SystemValue("Magnetic Field",magneticField.toString()))
lineNodevaluelist.add(
LinenNodeValueData(
"Internal Voltage","1V8: $internalVoltage1V8","Vbus rail: $internalVoltagevBusRail","VDD: $internalVoltageVDD","VDDH: $internalVoltageVDDH"
)
)
lineNodevaluelist.add(
LinenNodeValueData(
"Power Harvesting Voltage","Sol1: $powerHarvestingVoltageSol1","Sol2: $powerHarvestingVoltageSol2","E-Mppt: $powerHarvestingVoltageEmppt","H-Mppt: $powerHarvestingVoltageHmppt"
)
)
lineNodeListBle.add(
SystemValue(
"eHField","H-field: $eHFieldHField E-field: $eHFieldEField"
)
)
lineNodevaluelist.add(
LinenNodeValueData(
"Ambient Temperatures","Acc: $ambientTemperaturesAcc","IR: $ambientTemperaturesIR","Mag: $ambientTemperaturesMag","Mcu: $ambientTemperaturesMCU"
)
)
lineNodeListBle.add(SystemValue("openThreadCFG",openThreadCFG.toString()))
setupRecyclerViewSystemNode(binding.systemNodeRecyclerView,systemNodeList)
Timber.i("blueviewRecycler :: ${systemNodeList.size}")
setupRecylerViewLineNodeOne(binding.lineNodeRecyclerView,lineNodeListBle,lineNodevaluelist)
Timber.i("blueviewRecycler1 :: ${lineNodeListBle.size}+${lineNodevaluelist.size}")
}
祝大家有个美好的一天 亲切的问候 机器人
解决方法
这里有一个条件。
<div class="w-100" id="infographic-links">
<a style="border: 5px solid red" href="/link5" id="goals">AAA</a>
<a href="/link4" id="reach">BBB </a>
<a href="/link3" id="achieved">CCC </a>
<a href="/link2" id="webminar">DDD </a>
<a href="/link1" id="reporting-page">EEE </a>
<a href="/link" id="involved">FFF </a>
</div>
如果您向列表中添加新值,list.size 会更改并且条件为假。所以,只需删除一个条件:
.image-module {
&__full {
.container-fluid {
max-width: 100%;
padding-right: 0;
padding-left: 0;
}
}
.desktop_image {
display: none;
@include media-breakpoint-up(md) {
display: block;
}
}
.mobile_image {
display: block;
@include media-breakpoint-up(md) {
display: none;
}
}
.container-fluid {
position: relative;
}
#infographic-links {
position: absolute;
max-width: 100%;
overflow: visible;
top: 0;
left: 0;
bottom: 0;
right: 0;
a {
border: 5px solid red
&:hover {
box-shadow: 0 0 20px 0 rgb(20,43,40);
-webkit-box-shadow: 0 0 20px 0 rgb(20,40);
-moz-box-shadow: 0 0 20px 0 rgb(20,40);
transition: box-shadow 0.15s ease-in-out;
}
}
#goals {
height: 12.5%;
width: 44.95%;
position: absolute;
top: 12.3%;
left: 16.3%;
@include media-breakpoint-down(sm) {
height: 17.1%;
width: 79.8%;
top: 10.7%;
left: 10.2%;
}
}
#reach {
height: 11.75%;
width: 33.7%;
position: absolute;
top: 26.2%;
left: 16.3%;
@include media-breakpoint-down(sm) {
height: 7%;
width: 79.8%;
position: absolute;
top: 33.55%;
left: 10.2%;
}
}
#achieved {
height: 11.3%;
width: 67.4%;
position: absolute;
top: 44.2%;
left: 16.3%;
@include media-breakpoint-down(sm) {
height: 9.3%;
width: 79.7%;
position: absolute;
top: 46.75%;
left: 10.3%;
}
}
#webminar {
height: 17.4%;
width: 33.7%;
position: absolute;
top: 55.485%;
left: 16.3%;
@include media-breakpoint-down(sm) {
height: 11.75%;
width: 79.8%;
position: absolute;
top: 55.48%;
left: 10.2%;
}
}
#reporting-page {
height: 17.4%;
width: 33.7%;
position: absolute;
top: 55.48%;
left: 50%;
@include media-breakpoint-down(sm) {
height: 11.35%;
width: 79.8%;
position: absolute;
top: 67.8%;
left: 10.2%;
}
}
#involved {
height: 22%;
width: 67.35%;
position: absolute;
top: 73%;
left: 16.3%;
@include media-breakpoint-down(sm) {
height: 19.45%;
width: 79.8%;
position: absolute;
top: 79.15%;
left: 10.2%;
}
}
}
}
另外,我希望你只调用 if (it.size == 26) {
showlist(it)
}
一次。
关于您的 diff utils 实现,来自文档:
bleValueViewModel.getList().observe(viewLifecycleOwner) {
showlist(it)
}
当 DiffUtil 想要检查两个项目是否存在时调用 相同的数据。
showItems
由 DiffUtil 调用以判断两个对象是否代表相同 项目。
此外,您似乎应该重新实现 DIfUtils 回调:
areContentsTheSame(int oldItemPosition,int newItemPosition)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。