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

时间:2019-06-22 09:33:28

标签: java android android-recyclerview

我创建了一个RecyclerView来显示我已连接的每个节点。在RecyclerView中,我添加了两个按钮和一个TextView。 TextView将显示节点的当前状态,即节点当前处于打开还是关闭状态。节点正在通过MQTT发送其当前状态,我能够检测到我的应用程序中每个节点的当前状态,但无法根据它更新TextView。如何更新TextView?

我尝试添加新项目并删除上一个项目,但是它们都不适合我。我已经在检测到状态消息来自哪个节点的地方添加了代码。

enter image description here

//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();
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

我假设我们有一个List<String> data作为我们的数据源,并且我们有rvadapter这就是我们的RecyclerView.Adapter。我们将按照以下步骤进行更新:

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

此外,您可以更改项目范围。并插入新项目或删除项目,但必须调用notify...中的rvadapter相关功能 但是如果您重新创建data例如:

data = new ...,那么您必须使用notifydatasetchanged;