gpt4 book ai didi

android - 如何在关闭另一个 DialogFragment 后刷新 DialogFragment

转载 作者:行者123 更新时间:2023-11-30 00:35:52 30 4
gpt4 key购买 nike

我有一个 AppCompatActivity,它在某个时候显示一个 DialogFragment。在此对话框中,有些项目在删除之前需要确认。通过另一个 Yes/No DialogFragment 询问该确认。当用户在第二个对话框中单击"is"时,我希望第一个对话框刷新其 ListView(只需要更新适配器并调用其 notifyDataSetChanged 方法)。问题是我不知道什么时候更新 ListView 。

因为删除功能是从各种来源调用的,所以我在 Activity 级别实现了一个监听器接口(interface),并在我需要删除项目时从该接口(interface)调用“onDeleteRequest”事件,这就是打开确认的 Activity 对话框并执行实际删除。

因为我不太关心在不必要的情况下刷新 ListView,所以我尝试在 onResume 事件中更新列表,但是当我回到第一个对话框时没有调用该事件确认后取消。

所以我的问题是:我如何知道显示在对话框 A 顶部的对话框 B 何时被关闭,以便我可以相应地刷新对话框 A?

编辑:一些代码来支持我的问题:

我的 Activity 课:

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;
}

}

最佳答案

使用EventBus .

注册你的对话 A 来监听事件。当您关闭对话框 B 时发布一个事件并传递列表项的适配器位置或您要使用的任何数据来标识要删除的项目。在您的对话框 A 中编写一个函数来接收此事件,您可以在其中删除项目。

关于android - 如何在关闭另一个 DialogFragment 后刷新 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43419952/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com