在我的应用中,我想在应用栏中设置地点名称。
我从上一个活动和当前活动获得的地点的ID我希望通过ID获取此地点,我使用来自Google Api Places for Android的文档,但我的代码没有用。
public class PlaceInfoActivity extends AppCompatActivity implements OnConnectionFailedListener {
@BindView(R.id.placeinfo_toolbar) Toolbar mToolbar;
private static final String TAG = "place info";
private Place place;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_info);
ButterKnife.bind(this);
final String placeID = getIntent().getStringExtra("PLACE_ID");
setSupportActionBar(mToolbar);
...
Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (places.getStatus().isSuccess() && places.getCount() > 0) {
place = places.get(0);
Log.i(TAG, "Place found: " + myPlace.getName());
} else {
Log.e(TAG, "Place not found");
}
places.release();
}
});
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(place.getName()); // <- place is null
}
}
}
据我了解,方法getPlaceById()
的工作原理是异步任务,但是如何确定何时初始化place
并更改应用栏中的文本?谢谢!
答案 0 :(得分:0)
在代码中尝试此更改。您应该在回调中更新您的标题
Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (places.getStatus().isSuccess() && places.getCount() > 0) {
place = places.get(0);
Log.i(TAG, "Place found: " + myPlace.getName());
} else {
Log.e(TAG, "Place not found");
}
places.release();
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(place.getName()); // <- Move this to inside the callback
}
}
});