如何使用 ListView 显示更新的数据(例如电压)?

How do I use a ListView to show updated data such as voltage?

所以我想显示我的 OBD2 适配器获得的电压输入。现在我只是在处理程序中将电压显示为 textview,但我希望它显示在 listview 中,因为在添加越来越多的数据进行查看时更方便.

列表视图示例:


电压: 0,0 V


每分钟转数: 0


BTHandler.java

public void run() {
    OBDcmds();

    try {

        ModuleVoltageCommand voltageCommand = new ModuleVoltageCommand();

        while (!Thread.currentThread().isInterrupted()) {
            guiHandler(Constants.VOLTAGE_STATUS, 0, voltageCommand.getFormattedResult());

            try {
                voltageCommand.run(mmInStream, mmOutStream);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {...}
}

MainActivity.java

private Handler mHandler = new Handler() {
    TextView voltageView = null;

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case Constants.MESSAGE_STATE_CHANGE:
                switch (msg.arg1) {
                    case BTHandler.STATE_CONNECTED:
                        (...)
                        socView = (TextView) findViewById(R.id.socView);
                        break;
                    case (...)
                        break;
                }
                break;
            case Constants.VOLTAGE_STATUS:
                if (msg.obj != null && voltageView != null) {
                    voltageView.setText((String) msg.obj);
                }
                break;
        }
    }
};

您将需要为 ListView item, create an ArrayAdapter 创建自定义布局,并参考 ArrayAdapter 中列表视图项的自定义布局。

查看这些教程: