Android 如何在关闭另一个DialogFragment后刷新DialogFragment
我有一个Android 如何在关闭另一个DialogFragment后刷新DialogFragment,android,dialog,dialogfragment,Android,Dialog,Dialogfragment,我有一个AppCompatActivity,在某个时候,它会显示一个对话框片段。在此对话框中,有一些项目在删除前需要确认。通过另一个Yes/No对话框fragment询问确认。当用户在第二个对话框中单击Yes时,我希望第一个对话框刷新其列表视图(只需更新适配器并调用其notifyDataSetChanged方法)。问题是我不知道什么时候更新listview 因为删除功能是从各种来源调用的,所以我在活动级别实现了一个侦听器接口,每当我需要删除一个项目时,我都会从该接口调用一个“onDeleteRe
AppCompatActivity
,在某个时候,它会显示一个对话框片段
。在此对话框中,有一些项目在删除前需要确认。通过另一个Yes/No对话框fragment
询问确认。当用户在第二个对话框中单击Yes时,我希望第一个对话框刷新其列表视图
(只需更新适配器并调用其notifyDataSetChanged
方法)。问题是我不知道什么时候更新listview
因为删除功能是从各种来源调用的,所以我在活动级别实现了一个侦听器接口,每当我需要删除一个项目时,我都会从该接口调用一个“onDeleteRequest”事件,而这正是打开确认对话框并执行实际删除的活动
由于我不太关心在不必要的情况下刷新ListView,因此我尝试在onResume
事件中更新列表,但在取消确认后返回第一个对话框时,不会调用该事件
所以我的问题是:我如何知道对话框a顶部显示的对话框B何时被取消,以便我可以相应地刷新对话框a
编辑:一些代码来支持我的问题:
我的活动课:
public class MonthActivity
extends AppCompatActivity
implements OnEditCalendarsDialogListener
{
...
//That's where dialog A is shown
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
...
if (id == R.id.action_select_calendar) {
final CalendarSelection currentSelection = mCalendarSelectionAdapter.getCurrentCalendarSelection();
if (currentSelection != null) {
EditCalendarsDialogFragment dialogFragment = EditCalendarsDialogFragment.newInstance(currentSelection);
dialogFragment.show(getSupportFragmentManager());
}
return true;
}
return super.onOptionsItemSelected(item);
}
...
//OnEditCalendarsDialogListener interface implementation
//That's where Dialog B is shown over Dialog A
@Override
public void onEditCalendarsDialogDelete(long calendarID) {
final Repository repository = Repository.getInstance(this);
final Calendar calendar = repository.fetchOneByID(Calendar.class, calendarID);
if (calendar != null) {
YesNoDialog yesNoDialog = YesNoDialog.newInstance(this, R.string.yes_no_dialog_confirmation, R.string.yes_no_dialog_calendar_delete);
setCurrentOnDecisionClickListener(new OnPositiveClickListener() {
@Override
public boolean onPositiveClick(DialogInterface dialog) {
//Delete calendar
repository.delete(calendar);
//That's where I'd like to notify Dialog A that it needs to be refreshed
return true;
}
});
yesNoDialog.show(getSupportFragmentManager());
}
}
}
我的对话课
public class EditCalendarsDialogFragment
extends DialogFragment
{
private OnEditCalendarsDialogListener mDialogListener;
public static EditCalendarsDialogFragment newInstance(CalendarSelection calendarSelection) {
EditCalendarsDialogFragment dialog = new EditCalendarsDialogFragment();
Bundle arguments = new Bundle();
if (calendarSelection != null) {
arguments.putLong(KEY_ID, calendarSelection.getID());
}
else {
arguments.putLong(KEY_ID, 0L);
}
dialog.setArguments(arguments);
return dialog;
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mDialogListener = (OnEditCalendarsDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCalendarSelectionDialogListener");
}
}
...
private View getLayoutView() {
View rootView = getActivity().getLayoutInflater().inflate(R.layout.calendar_list, null, false);
if (rootView != null) {
mCalendars = (ListView) rootView.findViewById(R.id.calendars);
if (mCalendars != null) {
//Create adaptor
mCalendarAdapter = new ArrayAdapter<Calendar>(
getContext(),
android.R.layout.simple_list_item_2,
android.R.id.text1,
new ArrayList<Calendar>()
) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
final Calendar calendar = getItem(position);
if (calendar != null && calendar.hasID()) {
...
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mDialogListener != null) {
//That's where I request delete from calling activity
mDialogListener.onEditCalendarsDialogDelete(calendar.getID());
}
return true;
}
});
}
return view;
}
};
mCalendars.setAdapter(mCalendarAdapter);
refreshCalendarList();
}
}
return rootView;
}
}
public类EditCalendarsDialogFragment
扩展对话框片段
{
私有OnEditCalendarsDialogListener MDialGlistener;
公共静态编辑日历DialogFragment newInstance(日历选择日历选择){
EditCalendarsDialogFragment对话框=新建EditCalendarsDialogFragment();
Bundle参数=新Bundle();
if(日历选择!=空){
arguments.putLong(KEY_ID,calendarSelection.getID());
}
否则{
参数.putLong(键ID,0L);
}
设置参数(参数);
返回对话框;
}
...
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mDialogListener=(OnEditCalendarsDialogListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()+“必须实现OnCalendarSelectionDialogListener”);
}
}
...
私有视图getLayoutView(){
View rootView=getActivity().GetLayoutFlater().inflate(R.layout.calendar\u列表,null,false);
if(rootView!=null){
mCalendars=(ListView)rootView.findviewbyd(R.id.calendars);
if(mCalendars!=null){
//创建适配器
McAllendaAdapter=新阵列适配器(
getContext(),
android.R.layout.simple\u list\u item\u 2,
android.R.id.text1,
新ArrayList()
) {
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
视图=super.getView(位置、转换视图、父级);
最终日历=getItem(位置);
if(calendar!=null&&calendar.hasID()){
...
view.setOnLongClickListener(新视图.OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
如果(mDialogListener!=null){
//这就是我请求从呼叫活动中删除的地方
mDialogListener.onEditCalendarsDialogDelete(calendar.getID());
}
返回true;
}
});
}
返回视图;
}
};
设置适配器(McAllendarAdapter);
刷新日历列表();
}
}
返回rootView;
}
}
使用
注册对话框以收听事件。当您关闭对话框B时,发布一个事件并传递listitem的适配器位置或任何您想要用来标识要删除的项的数据。在对话框中编写一个函数来接收此事件,在该事件中删除该项。好的,因此我最终使用了“过度滥用回调”方法
我创建了以下界面:
public interface OnDeletedListener {
void onDeleted();
}
更新了OnEditCalendarsDialogListener接口,以便回调也具有对此接口的回调:
public interface OnEditCalendarsDialogListener {
void onEditCalendarsDialogDelete(long calendarID, OnDeletedListener onDeletedListener);
}
在“Dialog A”类中实现了OnDeletedListener接口:
最后,在接受并执行删除时调用回调:
public class MonthActivity
extends AppCompatActivity
implements OnEditCalendarsDialogListener
{
//OnEditCalendarsDialogListener interface implementation
//That's where Dialog B is shown over Dialog A
@Override
public void onEditCalendarsDialogDelete(long calendarID, final OnDeletedListener onDeletedListener) {
final Repository repository = Repository.getInstance(this);
final Calendar calendar = repository.fetchOneByID(Calendar.class, calendarID);
if (calendar != null) {
YesNoDialog yesNoDialog = YesNoDialog.newInstance(this, R.string.yes_no_dialog_confirmation, R.string.yes_no_dialog_calendar_delete);
setCurrentOnDecisionClickListener(new OnPositiveClickListener() {
@Override
public boolean onPositiveClick(DialogInterface dialog) {
//Delete calendar
repository.delete(calendar);
//That's where I notify Dialog A that it needs to be refreshed
if (onDeletedListener != null) {
onDeletedListener.onDeleted();
}
return true;
}
});
yesNoDialog.show(getSupportFragmentManager());
}
}
}
工作顺利 因为这是学校作业,所以我一直在寻找一个不涉及第三方图书馆的解决方案。但对于未来的项目来说,Publish-Suscriber设计模式的实现可能是有用的。我把它保存在书签里,以备将来参考。谢谢还有其他的方法,比如你说的界面。或者,在打开对话框b时,您可以传递对话框a的引用,并在删除某个项并将其取消时从对话框b调用对话框a的deleteitem方法。但请记住,在调用该方法以避免NPE之前,检查dialog a的实例是否不为null。
public class MonthActivity
extends AppCompatActivity
implements OnEditCalendarsDialogListener
{
//OnEditCalendarsDialogListener interface implementation
//That's where Dialog B is shown over Dialog A
@Override
public void onEditCalendarsDialogDelete(long calendarID, final OnDeletedListener onDeletedListener) {
final Repository repository = Repository.getInstance(this);
final Calendar calendar = repository.fetchOneByID(Calendar.class, calendarID);
if (calendar != null) {
YesNoDialog yesNoDialog = YesNoDialog.newInstance(this, R.string.yes_no_dialog_confirmation, R.string.yes_no_dialog_calendar_delete);
setCurrentOnDecisionClickListener(new OnPositiveClickListener() {
@Override
public boolean onPositiveClick(DialogInterface dialog) {
//Delete calendar
repository.delete(calendar);
//That's where I notify Dialog A that it needs to be refreshed
if (onDeletedListener != null) {
onDeletedListener.onDeleted();
}
return true;
}
});
yesNoDialog.show(getSupportFragmentManager());
}
}
}