如何使用ListView显示电压等更新数据?

时间:2016-04-26 12:40:04

标签: android listview

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

Listview示例:

电压:0,0 V

RPM: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;
        }
    }
};

1 个答案:

答案 0 :(得分:0)

您需要为ListView项创建自定义布局,创建ArrayAdapter,并参考ArrayAdapter中listview项的自定义布局。

查看这些教程: