ダイアログフレグメントでユーザーによって行われたアクションを受け取る方法は?

StackOverflow https://stackoverflow.com//questions/24031167

質問

連絡先のリストを表示するListViewがあります。連絡先をクリックすると、キャンセルまたは確認するか確認するかどうかについての通知が表示されます。[キャンセル]または[確認]をクリックするか確認したいのかをたどり、ListViewアイテムの色を変更したいと確認します。ボタンのステータスをクリックするにはダイアログをクリックします。ダイアログのマイコード:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        String first = this.getArguments().getString("first");
        String last = this.getArguments().getString("last");
        String phone = this.getArguments().getString("phone");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.add_contact_dialog, null);
        TextView tv = (TextView) view.findViewById(R.id.new_first);
        tv.setText(first);

        TextView tv2 = (TextView) view.findViewById(R.id.new_phone);
        tv2.setText(phone);

        TextView tv3 = (TextView) view.findViewById(R.id.new_last);
        tv3.setText(last);

        builder.setView(view);
        //builder.setTitle("Are you sure?").setMessage("Would you like to add " + name + " as a contact?");

        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Add the contact

                   }
               })
               .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });

        // Create the AlertDialog object and return it
        return builder.create();
    }
.

これで、SetPositiveButtonまたはSetNegativeButtonが私の主なアクティビティで比較できる値を返すか、またはダイアログの範囲で私のArrayAdapterを取得することで、変更を加えることができます。クリックされたDialogFragment.show()とダイアログボタンを関連付ける方法はありますか?

役に立ちましたか?

解決

ダイアログフレグメンメントを関連付ける方法はあります.show()とダイアログ クリックしたボタン?

インターフェースを使用して、その情報をアクティビティに渡すと、変更を行う可能性があるアダプタ/フラグメントに渡される可能性があります。

public interface OnSelectionMade {

     int OK = 1000;
     int CANCEL = 2000;
     doChange(int result);
}
.

このインタフェースを実装し、doChange()コールバックを使用して、アダプタに直接アクセスするか(単純なListViewを使用している場合)、またはその変更を実行するためにListViewを保持しているフラグメントに渡します。

public class YourActivity extends Activity implements OnSelectionMade {

     @Override
     public void doChange(int result) {
         // the result parameter will be either the OK or CANCEL constants
         // you know now what button was clicked so you can do the change
         ...
     }

}
.

このようにDialogFragmentを通じてイベントを渡すようにOnSelectionMadeを配信する必要があります。

private OnSelectionMade mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mListener = (OnSelectionMade) activity; // you'd want to implement a cast check here to be safe
}

//then in the onCreateDialog() method
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.doChange(OnSelectionMade.OK); // do a null mListener check?
                   }
               })
               .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.doChange(OnSelectionMade.CANCEL);
                   }
               });
.

結果を渡すために直接DialogFragmentへの参照を直接渡すことができますが、それは非常に良い解決策の長期ではありません。

他のヒント

はちがあります。Activityをインスタンス化している場所からのFragmentまたはDialogFragmentでは、最初に宣言します。

public static final int MY_DIALOGFRAGMENT = 123;   /* Any random int values will do. */
public static final int CONTACT_CONFIRM = 124;
public static final int CONTACT_DENY = 125;
.

DialogFragmentをそのようにインスタンス化する:

FragmentManager fm = getSupportFragmentManager();
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.setTargetFragment(this, MY_DIALOGFRAGMENT);
myDialogFragment.show(fm, "my_dialog_fragment");
.

onCreateDialog()DialogFragmentメソッド、

builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Add the contact
                   getTargetFragment().onActivityResult(getTargetRequestCode(), 
                       MyActivity.CONTACT_CONFIRM, getActivity().getIntent());
                   dismiss();
               }
           })
           .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
                   getTargetFragment().onActivityResult(getTargetRequestCode(), 
                       MyActivity.CONTACT_DENY, getActivity().getIntent());
                   dismiss();
               }
           });
.

そして最後に、ActivityまたはFragmentに次の方法を追加します。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case MY_DIALOGFRAGMENT:

                if (resultCode == CONTACT_CONFIRM) {

                    // Change color of listview item to green

                } else if (resultCode == CONTACT_DENY){

                    // Change color of listview item to red

                }

                break;
        }
}
.

これはうまくいくべきです。試してみてください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top