我正在尝试使用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()确实得到执行,因为我收到通知。

最佳答案

尝试在adapter中添加方法

public fun setDatass(datasList: List<Datas>){
    this.datasList= datasList
    notifyDataSetChanged()
}


并在activityfragment中调用它

    public void somefun(){
    Pump pump = new Pump("71xxxxxx", "East Motor", "OFF",R.drawable.motor);

    pumpList.add(pump);
    mAdapter.setDatas(pumpList);
}

关于java - mAdapter.notifyDataSetChanged()在某些情况下不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56087320/

10-09 04:07