简体   繁体   English

如何动态更改回收者视图项?

[英]How to change recycler view item dynamically?

I have created a RecyclerView to show each node that I have connected. 我创建了一个RecyclerView来显示我已连接的每个节点。 In the RecyclerView I have added two buttons and one TextView. 在RecyclerView中,我添加了两个按钮和一个TextView。 The TextView will show the current status of the node ie whether the node in currently on or off. TextView将显示节点的当前状态,即节点当前处于打开还是关闭状态。 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. 节点正在通过MQTT发送其当前状态,我能够检测到我的应用程序中每个节点的当前状态,但无法根据它更新TextView。 How can I update the TextView? 如何更新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 . 我假设我们有一个List<String> data作为我们的数据源,并且我们有rvadapter这是我们的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 : 并插入新项目或删除项目,但必须调用rvadapter notify...相关函数,但是如果您重新创建data ,例如:

data = new ... then you have to use notifydatasetchanged ; data = new ...然后您必须使用notifydatasetchanged ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不重新加载屏幕的情况下更改回收站视图中的单个项目? - How to change single item in recycler view without screen reload? 如何更改回收站视图中的字体? - How to change the font in recycler view? 如何在回收者视图的单个项目中更新视图 - how to update view in a single item of recycler view 如何在回收者视图中聚焦特定项目? - how to focus a specific item in recycler view? 如何在回收者视图中的项目之间添加分隔符? - How to add seprator between item in recycler view? 使用导航组件单击 RecyclerView 项目时如何从 RecyclerView OnClick 侦听器更改片段 - How to change fragment from the Recycler View OnClick Listener when RecyclerView item is clicked using Navigation Components 在回收站视图项目中单击增量按钮时,如何停止在回收站视图中自动向上滚动? - How to stop automatic scroll up in recycler view when increment button is clicked in recycler view item? 单击某个项目后如何关闭回收站视图持有人? - How to close recycler view holder after clicking an item? 如何在回收站视图中删除项目而不删除位置? - How to Delete Item Without Deleting Position in Recycler View? 如何在回收者视图的每个项目中使用地图 - How to use map in every item of my recycler view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM