排球响应后返回InfoWindow的视图 - Android

时间:2018-02-03 19:28:46

标签: android callback android-volley infowindow

我想在谷歌地图上显示一个信息窗口。此信息窗口应显示不同路径的用户高分。布局文件已设置,但在返回视图之前未及时更新。到目前为止,我已经使用Volley从我的数据库中检索数据。它以异步方式处理任务的优点在于这是一个缺点。

我已经看过callblack实现,在未来的凌空请求中,并且有一些关于线程休眠的玩法。我没有实现第一种和第三种方法,我找不到一个未来的截击请求的示例,该请求使用输入与数据库进行通信。这是我的代码,可以让您更好地了解我想要做的事情:

公共类MyInfoWindow实现了GoogleMap.InfoWindowAdapter {

Context context;
LayoutInflater inflater;
View view;

TextView runner_first_name_view;
TextView runner_second_name_view;
TextView runner_third_name_view;

TextView runner_first_time_view;
TextView runner_second_time_view;
TextView runner_third_time_view;

TextView route_average_time_view;
TextView route_length_view;
TextView route_keywords_view;


public MyInfoWindow(Context context) {

    this.context = context;
}


@Override
public View getInfoContents(Marker marker) {

    return null;
}

@Override
public View getInfoWindow(Marker marker) {

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.my_info_window, null);

    //Initialize route ID, which is saved in marker title
    final String routeID = marker.getTitle();

    //Initialize TextViews
    runner_first_name_view = view.findViewById(R.id.runner_first_name_view);
    runner_second_name_view = view.findViewById(R.id.runner_second_name_view);
    runner_third_name_view = view.findViewById(R.id.runner_third_name_view);

    runner_first_time_view = view.findViewById(R.id.runner_first_time_view);
    runner_second_time_view = view.findViewById(R.id.runner_second_time_view);
    runner_third_time_view = view.findViewById(R.id.runner_third_time_view);

    route_average_time_view = view.findViewById(R.id.route_average_time_view);
    route_length_view = view.findViewById(R.id.route_length_view);
    route_keywords_view = view.findViewById(R.id.route_keywords_view);

    //this is getting updated before returning the view
    route_length_view.setText(marker.getSnippet() + "km");

    //Asynchronous Volley request 
    String server_url = "http://<<ServerIP>>/loadInfoWindowData.php";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // response

                    String[] runner_names = new String[3];
                    String[] runner_times = new String[3];

                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        Log.d(TAG, "array converted");


                        for (int i = 0; i < 3; i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            runner_names[i] = jsonObject.getString("user_name");

                            runner_times[i] = jsonObject.getString("user_duration");

                            Log.d(TAG, "Array filled" + i);
                        }


                        //This is not getting updated in time before returning the view.
                        runner_first_name_view.setText(runner_names[0]);
                        runner_first_time_view.setText(runner_times[0]);

                        if (!runner_names[1].equals(null)) {
                            runner_second_name_view.setText(runner_names[1]);
                            runner_second_time_view.setText(runner_times[1]);

                            Log.d(TAG, "onResponse: " + runner_names[1] + "; textview 1: " + runner_second_name_view.getText());
                        }
                        if (!runner_names[2].equals(null)) {
                            runner_third_name_view.setText(runner_names[2]);
                            runner_third_time_view.setText(runner_times[2]);
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();

                        Log.d(TAG, "Process failed");
                    }
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error response
                    Toast.makeText(context, "Internet connection failed. Couldn't load user highscore." + error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("routeID", routeID);
            return params;
        }
    };

    MySingleton.getInstance(context).addToRequestQueue(stringRequest);

    return view;
}

}

我很感激任何建议!

2 个答案:

答案 0 :(得分:0)

来自the Info Windows documentation

  

注意:绘制的信息窗口不是实时视图。视图在返回时呈现为图像(使用View.draw(Canvas))。这意味着对地图的任何后续更改都不会反映在地图上的信息窗口中。

因此,您需要重新编写Volley回调以创建新的InfoWindow并显示该InfoWindow,而不是尝试更新现有的InfoWindow

答案 1 :(得分:0)

好的,我已经设法让它运转起来。我已经在我的主要活动中实现了一个onMarkerClicklistener,在那里我调用了一个执行凌空代码的方法。我的onMarkerClick返回false,因此它遵循默认程序将摄像机移动到标记并调用infoWinfow。

public boolean onMarkerClick(final Marker marker) {
    // TODO Auto-generated method stub

    MyInfoWindow.initializeHighscore(MainActivity.this, marker);

return false;

}

当凌空代码被执行时,我的信息Window类正在初始化默认情况下的View而没有凌空响应。在齐射完成他的作业marker.showInfoWindow();在凌空响应中调用,用来自数据库的数据更新infowindow。

...

public void onResponse(String response) {
                    // response

                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        Log.d(TAG, "array converted");


                        for (int i = 0; i < 3; i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            runner_names[i] = jsonObject.getString("user_name");

                            runner_times[i] = jsonObject.getString("user_duration");

                            Log.d(TAG, "Array filled" + i);
                        }

                        infoWindowCase = 1;

                        marker.showInfoWindow();

                        infoWindowCase = 0;

                        Log.d(TAG, "info window case reset");
                    } catch (JSONException e) {
                        e.printStackTrace();

                        Log.d(TAG, "Process failed");

                    }
                }
            },

...

希望这有助于某人。