我正在調用應用程序,在我的應用程序中我設置了一個報警任務,並在任務完成後在broadcastreceiver類中更新片段列表視圖。如何從BroadcastReceiver類更新片段ListView
我的片段類..
public class CallScheduling extends Fragment implements View.OnClickListener {
TextView messageText;
FloatingActionButton AddScheduleCall;
RecyclerView SheduleCalled;
View view;
DataBase dataBase;
List<SchedueModel> schedueModels;
public CallScheduling() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_call_scheduling, container, false);
setHasOptionsMenu(true);
init();
return view;
}
private void init() {
messageText = (TextView) view.findViewById(R.id.messageText);
AddScheduleCall = (FloatingActionButton) view.findViewById(R.id.AddScheduleCall);
SheduleCalled = (RecyclerView) view.findViewById(R.id.SheduleCalled);
AddScheduleCall.setOnClickListener(this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
SheduleCalled.setLayoutManager(mLayoutManager);
SheduleCalled.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void onResume() {
super.onResume();
dataBase = App.getInstance().getDatBase();
if (dataBase.checkForTables(Constant.TABLE_SCHEDULING)) {
messageText.setVisibility(View.GONE);
SheduleCalled.setAdapter(new ScheduleAdapter(getActivity()));
schedueModels = dataBase.GetAllScheduledCall();
Collections.sort(schedueModels, new Comparator<SchedueModel>() {
@Override
public int compare(SchedueModel schedueModel, SchedueModel t1) {
return schedueModel.getDate().compareTo(t1.getDate());
}
});
ActiveSchedul();
} else {
messageText.setVisibility(View.VISIBLE);
}
}
private void ActiveSchedul() {
for (int i = 0; i < schedueModels.size(); i++) {
if (schedueModels.get(i).getCompleted().equalsIgnoreCase("0")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(schedueModels.get(0).getDate());
} catch (ParseException e) {
e.printStackTrace();
}
long startDate = date.getTime();
Intent intentAlarm = new Intent(getActivity(), SchecduleBroadcast.class);
intentAlarm.putExtra("data", schedueModels.get(i));
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, startDate, PendingIntent.getBroadcast(getActivity(), 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
break;
} else {
boolean deleted = dataBase.deleteUser(schedueModels.get(i).getNumber(), Constant.TABLE_SCHEDULING, Constant.NUMBER);
if (deleted) {
schedueModels.remove(i);
SheduleCalled.getAdapter().notifyDataSetChanged();
}
}
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.AddScheduleCall:
startActivity(new Intent(getActivity(), ScheduleForm.class));
break;
default:
break;
}
}
,這是我的廣播接收器...
public class SchecduleBroadcast extends BroadcastReceiver {
SchedueModel schedueModel;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getSerializableExtra("data") != null) {
schedueModel = (SchedueModel) intent.getSerializableExtra("data");
Toast.makeText(context, schedueModel.getNumber() + " Is Completed ! Thanx", Toast.LENGTH_SHORT).show();
schedueModel.setCompleted("1");
App.getInstance().getDatBase().updateScheduledCall(schedueModel);
// new Home().updateFragment(CallScheduling.class, null);
}
}
}
您可以使用[EventBus(https://github.com/greenrobot/EventBus) –