如何在BroadcastReceiver中刷新片段ListView?

时间:2016-01-05 09:11:10

标签: android listview broadcastreceiver

有没有办法在收到数据时刷新BroadcastReceiver中的片段ListView?

这是ListView的Fragment扩展ListFragment中的刷新方法:

public void refreshTicketList() {

        list = readData.getAllRows();
        Collections.reverse(list);
        if (list.isEmpty()) {
            Toast.makeText(getContext(), "No data, Please Swipe Down to get data.", Toast.LENGTH_LONG).show();
        } else {
           adapter = new ListAdapter(getContext(), list);
            setListAdapter(adapter);
        }
    }

我如何在BroadcastReceiver中使用此方法?

1 个答案:

答案 0 :(得分:1)

我发现当BroadcastReceiver从服务器接收数据时,我们如何刷新ListView。

当BroadcastReceiver获取数据时,我们必须将消息发送到Activity或Fragment,如下所示:

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //Refresh code here
            }
        };

@Override
    public void onResume() {
        super.onResume();           
        getActivity().registerReceiver(broadcastReceiver, new IntentFilter("REFRESH"));
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadcastReceiver);
    }

全部;)