我想在recyclerview上实时更改数据

3xiyfsfu  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(361)

我正在尝试在我的recyclerview中每1秒刷新一次,但我不知道如何使用我的api进行刷新,当api中发生更改时,我想在没有刷卡器的recylerview中进行更改,或者我想让刷卡器每1秒刷卡一次。。这是我的密码:

public class InformationNow extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    private RequestQueue requestQueue;
    private SwipeRefreshLayout refresh;
    private ArrayList<TrashCanModel> trashCanModels = new ArrayList<>();
    private JsonArrayRequest arrayRequest;
    private Dialog dialog;
    private RecyclerView recyclerView;
    private  InformationNowAdapter informationNowAdapter ;
    private String url = "http://192.168.1.34:9090/TrashCanData/lastrow";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information_now);
        refresh = (SwipeRefreshLayout) findViewById(R.id.Swiper);
        recyclerView = (RecyclerView) findViewById(R.id.Now);

        dialog = new Dialog(this);

        refresh.setOnRefreshListener(this);
        refresh.post(new Runnable() {
            @Override
            public void run() {
                trashCanModels.clear();
                getData();
            }
        });

    }

    private void getData() {
        refresh.setRefreshing(true);

        arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                JSONObject jsonObject = null;
                for (int i = 0; i < response.length(); i++) {
                    try {
                        //JSONArray jsonArray = response.getJSONArray("users");
                        jsonObject = response.getJSONObject(i);
                        TrashCanModel cat = new TrashCanModel();
                        cat.setDistance(jsonObject.getInt("distance"));
                        cat.setTemperature(jsonObject.getInt("temperature"));
                        cat.setHumidity(jsonObject.getInt("humidity"));
                        Log.e("*********", cat.getDistance().toString());
                        Log.e("*********", cat.getTemperature().toString());
                        Log.e("*********", cat.getHumidity().toString());

                        trashCanModels.add(cat);
                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                }
                adapterPush(trashCanModels);
                refresh.setRefreshing(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue = Volley.newRequestQueue(InformationNow.this);
        requestQueue.add(arrayRequest);
    }

    private void adapterPush(ArrayList<TrashCanModel> trashCanModels) {

        informationNowAdapter = new InformationNowAdapter(this, trashCanModels);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(informationNowAdapter);

    }

    @Override
    public void onRefresh() {
        trashCanModels.clear();
        getData();

    }

}

和我的适配器:

public class InformationNowAdapter extends RecyclerView.Adapter<InformationNowAdapter.MyViewHolder>{

    private Context context ;
    private ArrayList<TrashCanModel> trashCanModels;
    private String url = "";
//

    public InformationNowAdapter (Context context , ArrayList<TrashCanModel> trashCanModels) {
        this.context = context;
        this.trashCanModels=trashCanModels;
    }

    @NonNull
    @Override
    public InformationNowAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view ;

        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.trashcan_list , parent , false);
        return new InformationNowAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InformationNowAdapter.MyViewHolder holder, int position) {
        //holder.id.setText("#"+String.valueOf(position+1));
        holder.distance.setText(trashCanModels.get(position).getDistance().toString());
        holder.temperature.setText(trashCanModels.get(position).getTemperature().toString());
        holder.humidity.setText(trashCanModels.get(position).getHumidity().toString());

    }

    @Override
    public int getItemCount() {
        return trashCanModels.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView  distance , temperature , humidity ;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            distance = (TextView) itemView.findViewById(R.id.distance);
            temperature = (TextView) itemView.findViewById(R.id.temperature);
            humidity = (TextView) itemView.findViewById(R.id.humidity);
//
        }
    }

}

所以我怎样才能使刷新数据每1秒!

piwo6bdm

piwo6bdm1#

你可以用定时器来做

Timer repeatTask = new Timer();
        repeatTask.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                trashCanModels.clear();
                getData();
            }
        }, 0, 1000);

呼叫 repeatTask.cancel() 当你想停止,但我认为你应该使用推送通知时,你的api改变或使用firebase或socket.io代替

相关问题