简体   繁体   中英

How to change recycler view item dynamically?

I have created a RecyclerView to show each node that I have connected. In the RecyclerView I have added two buttons and one TextView. The TextView will show the current status of the node ie whether the node in currently on or off. The nodes are sending their current status over MQTT, I am able to detect the current status of every node on my app but I am unable to update the TextView according to it. How can I update the TextView?

I have tried adding new item and deleting the previous one, but none of them works for me. I have added the code where I am detecting from which node the status message is coming.

在此处输入图片说明

//From this section I am getting details of my all node stored into the database
    public void addData()
        {
            pumpList = new ArrayList<>();

            StringRequest stringRequest = new StringRequest(Request.Method.GET, REQUEST_URL,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response)
                        {
                            try
                            {
                                JSONArray pumpData = new JSONArray(response);

                                for(int i = 0; i < pumpData.length(); i++)
                                {
                                    JSONObject pump = pumpData.getJSONObject(i);

                                    String name = pump.getString("name");
                                    String device_id =  pump.getString("device_id");
                                    String stat = pump.getString("stat");

                                    Log.d("HTTP",String.valueOf(name));
                                    Log.d("HTTP",String.valueOf(device_id));
                                    Log.d("HTTP",String.valueOf(stat));

                                    pumpList.add(new pumpData(name,device_id,stat));
                                }

                                pumpAdapter adapter = new pumpAdapter(getActivity(),pumpList);
                                recyclerView.setAdapter(adapter);
                                progressDialog.dismiss();
                            }
                            catch (JSONException e)
                            {
                                e.printStackTrace();
                                progressDialog.dismiss();
                            }
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error)
                        {
                            Toast.makeText(getActivity(),error.getMessage(),Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    });
            Volley.newRequestQueue(getActivity()).add(stringRequest);
        }

//When I get a message over MQTT then I am showing from which device the MQTT Message is Coming from
    public void changeDataByFiltering(String MQTT_TOPIC, String MQTT_PAYLOAD)
        {
            for(pumpData pD: pumpList)
            {
                insertStringSubscribe INSSUB = new insertStringSubscribe();
                String mqtt_topic_from_list = INSSUB.insertString(pD.getDeviceID());
                if(mqtt_topic_from_list.trim().toLowerCase().equals(MQTT_TOPIC.trim().toLowerCase()))
                [enter image description here][1]{
                    if(MQTT_PAYLOAD.trim().toLowerCase().equals(payloadON.trim().toLowerCase()))
                    {
                        Toast.makeText(mCtx, pD.getName()+": ON",Toast.LENGTH_SHORT).show();
                    }

                    if(MQTT_PAYLOAD.trim().toLowerCase().equals(payloadOFF.trim().toLowerCase()))
                    {
                        Toast.makeText(mCtx, pD.getName()+": OFF",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

I assume that we have a List<String> data as our data source and we have rvadapter which is our RecyclerView.Adapter . and we will do as below to update an item:

data.set(position,"newdata");
rvadapter.notifyItemChanged(position);

Also, you can change the range of items. and insert new item or delete item but have to call notify... related function in rvadapter but if you recreate the data for example :

data = new ... then you have to use notifydatasetchanged ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM