2016-02-20 27 views
0

所以我一直在嘗試使用estimote信標,並一直在學習如何使用它的教程幫助... 但最終,他們說,一個需要在列表視圖中使用哈希映射... 代碼如下: private static final Map> PLACES_BY_BEACONS;顯示信息在列表視圖(Estimote燈塔)

// 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(); 
     } 
    }); 
} 

所以我的問題是我怎麼顯示所有在placesByBeacons在我的UI對象(列表視圖),它是由LV在代碼中提及。

+0

你可能在這裏尋找一個'ListView'教程...? –

+0

這不是我正在尋找的listview教程。我試圖用一個陣列適配器來解決它,但它不起作用。所以我需要一些其他的代碼... –

+0

你嘗試過'BaseAdapter'嗎?看看這裏的示例實現http://stackoverflow.com/questions/35495312/what-other-adapters-can-i-use-for-listview/35495669#35495669 –

回答

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,即,該代碼假定ListView在步驟1中添加有ID listView

  3. 正下方的「TODO:在此處更新UI,」補充:

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

    這取代了數據備份與燈塔測距結果產生的最新鮮的數據列表視圖。