首页 > 解决方案 > 如何在 FindCurrentPlaceResponse 中获取可以稍后单独选择的单个响应

问题描述

我需要进行一项活动,允许用户单击 getLocation 按钮,该按钮又根据可能性得分返回地点列表。FindCurrentPlaceResponse 的响应返回 20 个项目,我希望它们以列表或卡片视图的形式出现,用户最终从中选择最合适的位置。如何使响应返回可以放入可点击列表的各个地方?

我已经关注并完全使用了https://github.com/googlemaps/android-places-demos上的 google places 文档来进行我的活动

这是文档中将列表转换为单个字符串的确切代码

@RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
private void findCurrentPlaceWithPermissions() {
setLoading(true);

    FindCurrentPlaceRequest currentPlaceRequest = FindCurrentPlaceRequest.newInstance(getPlaceFields());
    Task<FindCurrentPlaceResponse> currentPlaceTask = placesClient.findCurrentPlace(currentPlaceRequest);


    currentPlaceTask.addOnSuccessListener(
        (response) ->
            responseView.setText(StringUtil.stringify(response, isDisplayRawResultsChecked())));

    currentPlaceTask.addOnFailureListener(
        (exception) -> {
          exception.printStackTrace();
          responseView.setText(exception.getMessage());
        });

    currentPlaceTask.addOnCompleteListener(task -> setLoading(false));
  }

标签: javaandroidgoogle-maps

解决方案


Stringify 就是这样做的。响应本身已经是“响应”。您可能需要一个带有回调的接口来将结果添加到图块列表中。

// The callback interface
interface MyCallback {
void callbackCall(List<String> response);
}

// The class that takes the callback
class Worker {
MyCallback callback;

void onEvent(List<String> response) {
  callback.callbackCall(response);
   }
}

// Option 1: MainActivity or where the list is

class Callback implements MyCallback {
  void callbackCall() {
   // callback code goes here
   // update your adaptor 


   }
 }

worker.callback = new Callback();

// Option 2:

worker.callback = new MyCallback() {

void callbackCall(List<String> results ) {
  // callback code goes here

   }
};

推荐阅读