[英]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.