我正在尝试使用RecyclerView
更新我的adapter.notifyDataSetChanged()
。当我使用按钮更新列表并在视图中反映出来时它起作用,但是当我从名为messageArrived()
的 mqttcall函数其他函数中调用它时,它不起作用。
我尝试重新初始化整个列表和适配器,但是当我通过单击按钮进行操作时,从messageArrived()
函数中调用时仍然无法正常工作。
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
somefun();
}
});
public void somefun(){
Pump pump = new Pump("71xxxxxx", "East Motor", "OFF",R.drawable.motor);
pumpList.add(pump);
mAdapter.notifyDataSetChanged();
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.d("In main Activity","Message arrived");
String s = message.toString();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle(topic)
.setContentText(s)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
somefun();
}
messageArrived()
确实会在收到消息时执行,因为我收到了通知。
答案 0 :(得分:0)
尝试在adapter
public fun setDatass(datasList: List<Datas>){
this.datasList= datasList
notifyDataSetChanged()
}
并在activity
或fragment
public void somefun(){
Pump pump = new Pump("71xxxxxx", "East Motor", "OFF",R.drawable.motor);
pumpList.add(pump);
mAdapter.setDatas(pumpList);
}
答案 1 :(得分:0)
正如Jeel所建议的那样,messageArrived()在后台线程上运行。在UI线程上运行notifyDataSetChanged()
对我有用。