我有一个(Integer; PlayerMarkerAndStatus)对的hashmap,用于存储Player id和PlayerMarkerAndStatus对象。 PlayerMarkerAndStatus具有在HashMap中指定的玩家ID的标记和当前状态。
我还有一个针对API的截击请求,它返回了关于排球成功案例的每个玩家的详细信息 - OnSuccess(Player2 nearbyPlayer)
。当点击标记时,应该将这些数据发送到片段 - onMarkerClick(Marker marker)
。
它将点击的标记与hashmap中的标记进行比较。如果它们具有相同的标记ID,则会将此播放器的详细信息发送到片段。但是,OnMarkerClickListener仅将HashMap的最后一个标记与单击的标记进行比较。
这是我的代码:
for (final Map.Entry<Integer, PlayerMarkerAndStatus> e :
player_id_marker_status.entrySet()) {
Log.d(LOG_TAG, "marker id of current entry: " + e.getValue().getMarker().getId());
getNearbyPlayerDetails(new VolleyCallbackNearbyPlayerDetails() {
@Override
public void onSuccess(final Player2 nearbyPlayer) {
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Log.d(LOG_TAG, "clicked marker id: " + marker.getId());
Log.d(LOG_TAG, "current marker id: " + e.getValue().getMarker().getId());
if (marker.getId().equals(e.getValue().getMarker().getId())) {
Log.d(LOG_TAG, "marker clicked. Player id: " + e.getKey());
PlayerDetailsFragment playerDetailsFragment =
new PlayerDetailsFragment();
Bundle bundle = new Bundle();
bundle.putString("player_details_username",
nearbyPlayer.getSurname());
if (nearbyPlayer.getType() == -1)
bundle.putString("player_details_type", "a");
else {
bundle.putString("player__details_type", "b");
}
bundle.putInt("player_details_health",
nearbyPlayer.getHealth());
playerDetailsFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.player_details_fragment_container,
playerDetailsFragment).commit();
return true;
}
return false;
}
});
}
}, e.getKey());
}
HashMap的日志:
09-22 11:37:42.410 23553-23553/com.orujzade.asdf D/MainActivity: marker id of current entry: m3
09-22 11:37:42.415 23553-23553/com.orujzade.asdf D/MainActivity: marker id of current entry: m2
09-22 11:37:42.415 23553-23553/com.orujzade.asdf D/MainActivity: marker id of current entry: m1
登录标记点击:
09-22 11:39:15.353 23553-23553/com.orujzade.asdf D/MainActivity: clicked marker id: m3
09-22 11:39:15.353 23553-23553/com.orujzade.asdf D/MainActivity: current marker id: m1
09-22 11:39:20.593 23553-23553/com.orujzade.asdf D/MainActivity: clicked marker id: m2
09-22 11:39:20.593 23553-23553/com.orujzade.asdf D/MainActivity: current marker id: m1
09-22 11:39:25.593 23553-23553/com.orujzade.asdf D/MainActivity: clicked marker id: m4
09-22 11:39:25.593 23553-23553/com.orujzade.asdf D/MainActivity: current marker id: m1
如何让OnMarkerClickListener
从散列图中取出正确的标记?
答案 0 :(得分:0)
setOnMarkerClickListener
应该不在循环中。为玩家ID和idPlayerMap
创建一个哈希地图Player2
。在齐射成功响应中,将Player2
对象的id放在该地图中。在MarkerClickListener
运行循环以检查点击了哪个标记并从idPlayerMap
获取相应的播放器并将其发送到所需的Fragment
。
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Log.d(LOG_TAG, "clicked marker id: " + marker.getId());
Log.d(LOG_TAG, "current marker id: " + e.getValue().getMarker().getId());
for (final Map.Entry<Integer, PlayerMarkerAndStatus> e :
player_id_marker_status.entrySet()) {
Log.d(LOG_TAG, "marker id of current entry: " + e.getValue().getMarker().getId());
if (marker.getId().equals(e.getValue().getMarker().getId())) {
Log.d(LOG_TAG, "marker clicked. Player id: " + e.getKey());
PlayerDetailsFragment playerDetailsFragment =
new PlayerDetailsFragment();
Player2 nearbyPlayer = idPlayerMap.get(e.getKey());
Bundle bundle = new Bundle();
bundle.putString("player_details_username",
nearbyPlayer.getSurname());
if (nearbyPlayer.getType() == -1)
bundle.putString("player_details_type", "a");
else {
bundle.putString("player__details_type", "b");
}
bundle.putInt("player_details_health",
nearbyPlayer.getHealth());
playerDetailsFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.player_details_fragment_container,
playerDetailsFragment).commit();
return true;
}
return false;
}
}
HashMap<Integer, Player2> idPlayerMap = new HashMap<Integer, Player2>();
for (final Map.Entry<Integer, PlayerMarkerAndStatus> e :
player_id_marker_status.entrySet()) {
Log.d(LOG_TAG, "marker id of current entry: " + e.getValue().getMarker().getId());
getNearbyPlayerDetails(new VolleyCallbackNearbyPlayerDetails() {
@Override
public void onSuccess(Player2 nearbyPlayer) {
idPlayerMap.put(e.getKey(), nearbyPlayer);
});
}
}
}
答案 1 :(得分:0)
将一个HashMap定义到类级别以从标记id获取玩家:
HashMap<String, Player2> playerMap = new HashMap<>();
迭代HashMap
并从上面定义方法获取玩家:
for (final Map.Entry<Integer, PlayerMarkerAndStatus> e : player_id_marker_status.entrySet()) {
Log.d(LOG_TAG, "marker id of current entry: " + e.getValue().getMarker().getId());
getNearbyPlayer(e.getKey(),e.getValue().getMarker().getId());
}
定义一种方法,让玩家使用排球请求,并使用标记作为关键将玩家设置为HashMap
:
public void getNearbyPlayer(Integer playerId,final String markerId) {
getNearbyPlayerDetails(new VolleyCallbackNearbyPlayerDetails() {
@Override
public void onSuccess(final Player2 nearbyPlayer) {
playerMap.put(markerId, nearbyPlayer);
}
},playerId);
}
最后定义标记点击监听器并从标记ID获取播放器:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Log.d(LOG_TAG, "clicked marker id: " + marker.getId());
Player2 nearbyPlayer = playerMap.get(marker.getId());
PlayerDetailsFragment playerDetailsFragment = new PlayerDetailsFragment();
Bundle bundle = new Bundle();
bundle.putString("player_details_username", nearbyPlayer.getSurname());
if (nearbyPlayer.getType() == -1){
bundle.putString("player_details_type", "a");
}else {
bundle.putString("player__details_type", "b");
}
bundle.putInt("player_details_health", nearbyPlayer.getHealth());
playerDetailsFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.player_details_fragment_container, playerDetailsFragment).commit();
return true;
}
});