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

时间:2016-02-20 20:04:04

标签: android listview estimote

所以我一直在尝试使用estimote信标,并在他们的教程的帮助下学习如何使用它... 但最后,他们说需要在listview中使用显示散列图... 这是代码:     private static final Map> 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();
        }
    });
}

所以我的问题是如何显示我的UI(Listview)中的placesByBeacons对象中的所有内容,这些对象在代码中由lv引用。

1 个答案:

答案 0 :(得分:1)

  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标识为listView

  3. 在&#34; TODO下面:在这里更新用户界面,&#34;添加:

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

    这将使用信标范围结果生成的最新数据替换列表视图后面的数据。