Android ListView 显示设备列表 - 示例代码
您可以使用 ListView 来显示设备列表。以下是一个示例代码:
- 在布局文件(例如 activity_main.xml)中添加 ListView 组件:
<ListView
android:id="@+id/deviceListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- 在活动中,找到 ListView 并将设备列表适配器设置为 ListView 的适配器:
ListView deviceListView = findViewById(R.id.deviceListView);
List<String> deviceList = findDevices(); // 用于获取设备列表的方法,返回一个字符串列表
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, deviceList);
deviceListView.setAdapter(adapter);
在上述代码中,findDevices() 是一个自定义的方法,用于获取设备列表并返回一个字符串列表。您可以根据您的需求自定义此方法。
- 最后,确保您的设备列表在适配器中更新时,调用
adapter.notifyDataSetChanged(),以便在列表中显示新的设备。
请注意,上述代码仅提供了一个基本的示例,您可以根据自己的需求进行修改和扩展。
原文地址: https://www.cveoy.top/t/topic/o6m8 著作权归作者所有。请勿转载和采集!