如何每5秒刷新一次gridview?

时间:2017-07-21 05:46:28

标签: java android multithreading gridview

我想每隔5秒刷新一次网格视图。当我第一次启动我的应用程序然后spinnerBus.setOnItemSelectedListener正在调用时,一切都很好,我的gridview也每5秒刷新一次,但是当我从spinnerBus中选择另一个项目时,我的网格视图开始闪烁。我想刷新网格视图而不闪烁。

spinnerBus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                try {

                    busid = spinnerBus.getSelectedItem().toString();

                    apiDrawRoute(shift_id, busid);
                    final Handler handler1 = new Handler();

                    Runnable refresh = new Runnable() {

                        @Override
                        public void run() {
                            apiRoutes(shift_id, stop_id, busid);
                            handler1.postDelayed(this, 5000);
                        }
                    };
                    handler1.postDelayed(refresh, 5000);

                } catch (Exception e) {

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


public void apiRoutes(String shift_id, String stop_id, String busid) {

        String tag_json_obj = "json_obj_req";


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("route : ", response.toString());
                        try {
                            JSONObject mainObj = new JSONObject(String.valueOf(response));
                            if (mainObj != null) {
                                routeList.clear();
                                JSONArray list = mainObj.getJSONArray("Message");
                                if (list != null) {
                                    for (int i = 0; i < list.length(); i++) {
                                        JSONObject elem = list.getJSONObject(i);
                                /*if(elem != null){
                                    JSONObject prods = elem.getJSONObject("running_status_model");*/
                                        RoutesModel routes = new RoutesModel();
                                        routes.setSrNo(elem.getString("rn"));
                                        routes.setName(elem.getString("location"));
                                        routes.setTime(elem.getString("stop_id"));
                                        routes.setStatus(elem.getString("route_id"));
                                        routes.setTxtEsTime(elem.getString("bus_id"));
                                        routes.setReamarks(elem.getString("device_id"));
                                        routeList.add(routes);

                                        //}
                                    }
                                }
                            }
                            adapter = new CustomListAdapter(getActivity(), routeList);
                            adapter.notifyDataSetChanged();
                            gridView.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //VolleyLog.d("route : ", "Error: " + error.getMessage());
                //Toast.makeText(getActivity(), "Sorry something went wrong.. try again", Toast.LENGTH_SHORT).show();

            }
        });

// Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    }

0 个答案:

没有答案