2017-04-14 21 views
0

AppCompatActivityがあり、ある時点でDialogFragmentと表示されています。このダイアログには、削除する前に確認を求める項目があります。その確認は、別のはい/いいえDialogFragmentによって尋ねられます。ユーザーがその2番目のダイアログで[はい]をクリックすると、最初のダイアログでListViewを更新したい(アダプタを更新してnotifyDataSetChangedメソッドを呼び出すだけでよい)。問題は、リストビューを更新する時期がわからないことです。別のDialogFragmentを閉じた後でDialogFragmentを更新する方法

さまざまなソースから削除機能が呼び出されているため、アクティビティレベルでリスナーインターフェイスを実装し、アイテムを削除する必要があるときはいつでもそのインターフェイスから「onDeleteRequest」イベントを呼び出します。確認ダイアログを開き、実際の削除を実行します。

不要な状況でListViewをリフレッシュすることについてはあまり気にしないので、onResumeイベントのリストを更新しようとしましたが、確認が却下された後に最初のダイアログに戻ったときにイベントは呼び出されません。

私の質問は次のとおりです。ダイアログAの上に表示されるダイアログBが終了したときに、それに応じてダイアログAを更新する方法を知る方法はありますか?

EDIT:

マイアクティビティクラス:

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

} 

答えて

0

OKですので、私はついには "over-abusive-callback"メソッドを使いました。

私は、次のインターフェイスを作成:コールバックは、あまりにも、このインターフェイスへのコールバックを持つように

public interface OnDeletedListener { 
    void onDeleted(); 
} 

はOnEditCalendarsDialogListenerインタフェースを更新:

public interface OnEditCalendarsDialogListener { 
    void onEditCalendarsDialogDelete(long calendarID, OnDeletedListener onDeletedListener); 
} 

は「ダイアログA」クラスのOnDeletedListenerインタフェースを実装:

public class EditCalendarsDialogFragment 
     extends DialogFragment 
     implements OnDeletedListener 
{ 
    ... 

    //OnDeletedListener interface implementation 

    @Override 
    public void onDeleted() { 
     //That's where I'm called back after item is deleted 
     refreshCalendarList(); 
    } 

    ... 

    private View getLayoutView() { 
     ... 
     view.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       if (mDialogListener != null) { 
        //That's where I request delete from calling activity, asking to call me back once deleted 
        mDialogListener.onEditCalendarsDialogDelete(calendar.getID(), EditCalendarsDialogFragment.this); 
       } 
       return true; 
      } 
     }); 
     ... 
    } 
} 

最後に、削除が受け入れられ実行されたときのコールバック:

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

スムーズに動作します!

1

使用EventBus私の質問をサポートするためのコードのビット。

イベントを聴くためにダイアログAを登録してください。ダイアログBを閉じると、イベントがポストされ、リストアイテムのアダプターの位置や、削除するアイテムを識別するために使用するデータが渡されます。ダイアログの内側に、このイベントを受け取るための関数を記述します。その中にアイテムを削除します。

+0

私は学校の割り当てのため、私は第三者図書館に関係しない解決策を探していました。しかし、今後のプロジェクトでは、Publish-Suscriberの設計パターンの実装が有用であるかもしれません。私は将来の参照のためにブックマークに入れました。ありがとう! – STremblay

+0

また、他の方法もあります。ダイアログbを開いている間は、ダイアログaの参照を渡して、項目を削除して閉じるときにダイアログbのダイアログaのdeleteitemメソッドを呼び出すことができます。しかし、NPEを回避するメソッドを呼び出す前に、ダイアログのインスタンスがnullでないかどうかを確認することを忘れないでください。 –

関連する問題