在列表视图中显示信息(Estimote Beacon)

Display information in listview (Estimote Beacon)

所以我一直在尝试使用 estimote beacon 并且一直在学习如何在他们的教程的帮助下使用它... 但是最后,他们说需要在列表视图中使用显示哈希图... 这是代码: 私人静态最终地图> PLACES_BY_BEACONS;

// TODO: replace "<major>:<minor>" strings to match your own beacons.
static {
    Map<String, List<String>> placesByBeacons = new HashMap<>();
    placesByBeacons.put("29098:1493", new ArrayList<String>() {{
        add("Heavenly Sandwiches");
        // read as: "Heavenly Sandwiches" is closest
        // to the beacon with major 22504 and minor 48827
        add("Green & Green Salads");
        // "Green & Green Salads" is the next closest
        add("Mini Panini");
        // "Mini Panini" is the furthest away
    }});
    placesByBeacons.put("52504:13020", new ArrayList<String>() {{
        add("Mini Panini");
        add("Green & Green Salads");
        add("Heavenly Sandwiches");
    }});
    PLACES_BY_BEACONS = Collections.unmodifiableMap(placesByBeacons);
}

private List<String> placesNearBeacon(Beacon beacon) {
    String beaconKey = String.format("%d:%d", beacon.getMajor(), beacon.getMinor());
    if (PLACES_BY_BEACONS.containsKey(beaconKey)) {
        return PLACES_BY_BEACONS.get(beaconKey);
    }
    return Collections.emptyList();
}



private BeaconManager beaconManager;
private Region region;
ListView lv;

 Button reserveButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    lv=(ListView)findViewById(R.id.listView);
    reserveButton=(Button)findViewById(R.id.button);


    beaconManager = new BeaconManager(this);
    beaconManager.setRangingListener(new BeaconManager.RangingListener() {
        @Override
        public void onBeaconsDiscovered(Region region, List<Beacon> list) {
            if (!list.isEmpty()) {
                Beacon nearestBeacon = list.get(0);
                List<String> places = placesNearBeacon(nearestBeacon);
                // TODO: update the UI here
                Log.d("Airport", "Nearest places: " + places);
            }
        }
    });


    region = new Region("ranged region",
            UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null);






    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Message your friends", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

所以我的问题是如何在代码中由 lv 引用的 UI(列表视图)中显示 placesByBeacons 对象中的所有内容。

  1. 在布局中添加 ListView

  2. 将此代码放入 onCreate:

    ListView listView = (ListView) findViewById(R.id.listView);
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(
            this, android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);
    

    这从您的布局中获取列表视图,并使用字符串集合的适配器对其进行设置,这正是 places 的内容。 (它对列表视图中的项目使用 Android 预定义布局,which is actually just a TextView。)

    注意 R.id.listView,即此代码假定您在步骤 1 中添加的 ListView 具有 ID listView

  3. 在 "TODO: update the UI here," 下方添加:

    adapter.clear();
    adapter.addAll(places);
    

    这会将支持列表视图的数据替换为从信标测距结果生成的最新数据。