如何使用新数据更新 ListAdapter

如何解决如何使用新数据更新 ListAdapter

我有一个 Modbus BLE 应用程序,它接收 Modbus 数据并在 LisAdapter 中显示数据。用户点击一个按钮,一个 Modbus 消息被发送到一个设备,然后从该设备接收一些数据。

收到此数据后,我将数据存储在一个类中并启动一个 Listadapter 来显示这些值。这一切正常。

然后我添加了一个线程,该线程在单击该按钮时继续请求 Modbus 数据。所以我现在每秒都会收到 Modbus 数据。收到的每个 Modbus 消息都会覆盖此数据。所以现在我需要告诉 LisAdapter 数据已更改以显示新的数据集。 - 需要添加到列表中的不是额外的数据,它是相同的数据,只是不同的值。

这是存储 Modbus 接收消息并启动包含 ListAdapter 的类的部分

Input_Reg.reserved_Plus1 = (rx_buff[4] << 8 | rx_buff[3]);
Input_Reg.reserved_HB = (rx_buff[6] << 8 | rx_buff[5]);                                           Input_Reg.statusCode = (rx_buff[8] << 8 | rx_buff[7]);                                           
Input_Reg.warningCode = (rx_buff[10] << 8 | rx_buff[9]);                                           
Input_Reg.statusWord = (rx_buff[12] << 8 | rx_buff[11]);                                               Input_Reg.iL_LevelMaxPer = (rx_buff[14] << 8 | rx_buff[13]);                                           Input_Reg.iL1_PerLev = (rx_buff[16] << 8 | rx_buff[15]);                                            
Input_Reg.iL2_PerLev = (rx_buff[18] << 8 | rx_buff[17]);                                          
Input_Reg.iL3_PerLev = (rx_buff[20] << 8 | rx_buff[19]);       
                                        
Intent intent = new Intent(context,ListView_InputReg.class);
intent.putExtra("Input_RegBlock1_RX",block_req);
context.startActivity(intent);

这是包含 LiastAdapter 的 Activity

package com.example.ble;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ListView_InputReg extends AppCompatActivity {
    private ArrayAdapter aAdapter;
    private ArrayList<String> list = new ArrayList<String>();
    // A List of type Listview_Strings for holding list items
    List<Listview_Strings> input_list;
    // The listview
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_input_reg);
        // Get Input Tree to display in listview
        Intent intent = getIntent();
        int block_rx = intent.getIntExtra("Input_RegBlock1_RX",0);
        // Initializing objects
        input_list = new ArrayList<>();
        listView = (ListView)findViewById(R.id.inputlistView);
        switch (block_rx){
            case 1:
                // Adding Input Register values to input_list (Exclude Reserved Words)
                input_list.add(new Listview_Strings("StatusCode",Input_Reg.statusCode.toString()));
                input_list.add(new Listview_Strings("WarningCode",Input_Reg.warningCode.toString()));
                input_list.add(new Listview_Strings("StatusWord",Input_Reg.statusWord.toString()));
                input_list.add(new Listview_Strings("IL Max Percentage",Input_Reg.iL_LevelMaxPer.toString()));
                input_list.add(new Listview_Strings("IL 1 Percentage",Input_Reg.iL1_PerLev.toString()));
                input_list.add(new Listview_Strings("IL 2 Percentage",Input_Reg.iL2_PerLev.toString()));
                input_list.add(new Listview_Strings("IL 3 Percentage",Input_Reg.iL3_PerLev.toString()));
                input_list.add(new Listview_Strings("IL Unbalance",Input_Reg.iL_UnbalanceLevel.toString()));
                input_list.add(new Listview_Strings("Positive Seq",Input_Reg.iL_PosSeqLev.toString()));
                input_list.add(new Listview_Strings("Negative Seq",Input_Reg.iL_NegSeqLev.toString()));
                input_list.add(new Listview_Strings("Zero Seq",Input_Reg.iL_ZeroSeqLev.toString()));
                input_list.add(new Listview_Strings("VL1 MinLev",Input_Reg.vL1_WordMinLev.toString()));
                input_list.add(new Listview_Strings("VL1 Level",Input_Reg.vL1_WordLev.toString()));
                input_list.add(new Listview_Strings("VL2 Level",Input_Reg.vL2_WordLev.toString()));
                input_list.add(new Listview_Strings("VL3 Level",Input_Reg.vL3_WordLev.toString()));
                // Creating the adapter
                InputCustomViewAdapter adapter = new InputCustomViewAdapter(this,R.layout.input_custom_list,input_list);
                // Attaching adapter to the listview
                listView.setAdapter(adapter);

                break;
            case 2:

                // Tree 2 Code
                break;
            case 3:

                // Tree 3 Code
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}

将更改的数据通知 ListAdapter 的正确方法是什么?

  1. adapter.clear();适配器.addAll(input_list); 或者只是
  2. adapter.notifyDataSetChanged();

还有我需要把这个通知放在哪里?

  1. 最后我在哪里收到 Modbus 消息? (无需再次调用 ListView.Class)
  2. 还是在 ListView_InputReg 类的 onCreate() 中?
  3. 还是在 ListView_InputReg 类的 onResume() 中?

我的尝试创建了多个 ListAdapter,因为我一直将 Intent 传递给 ListView_InputReg 类

马里纳斯

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-