我无法从BroadcastReceiver类中删除mycardview项目

时间:2018-10-27 21:23:41

标签: android broadcast cardview

我在myFragment中使用cardview。当myFragment收到广播时,我想在cardview中删除一个项目。当myFragment收到广播时,我将调用remove和notifyItemRemoved。但是cardview中的项目不会被删除。为了进行试用,我在myFragment的onViewCreated中创建了一个按钮侦听器,以调用remove和notifyItemRemoved。然后我按下此按钮,cardview中的项目将被删除... 与从BroadcastReceiver和按钮侦听器进行调用有什么不同??

myFragment

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_dash_board, container, false);
    if(mitems.size() != 0) {
        recyclerView = view.findViewById(R.id.recycle_view_noticeItems);
        recyclerView.setHasFixedSize(true);
        Context context = getContext();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        mviewadapter = new NoticeItemsViewAdapter(mitems);
        recyclerView.setAdapter(mviewadapter);
    }

    IntentFilter messageFilter = new IntentFilter("AlarmEvent");
    DataReceiver dataReceiver = new DataReceiver();

    LocalBroadcastManager.getInstance(context)
            .registerReceiver(dataReceiver, messageFilter);

    return view;

myFragment中的DataReceiver类

    public class DataReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("DataReceiver", "onReceive");
        String id = intent.getStringExtra("id");

        //I would like to remove an item in cardview here.
        //mitems and mviewadapter are member of myFragment.
        mitems.remove(0);
        mviewadapter.notifyItemRemoved(0);
    }

0 个答案:

没有答案