1

私はAndroid開発の初心者です。メイン アクティビティから Number Picker のアラート ダイアログを開き、アラート ダイアログから入力を取得してメイン ビューに表示したいと考えています。

いくつかの参照を取得してコードを作成し、正しく機能しました。しかし、メインアクティビティで「implements NumberPickerFragment.NoticeDialogListener」を使用したくありません。どうすれば値をメインアクティビティに戻すことができますか。

メインアクティビティの私のコードは次のとおりです。

    package com.pinnacleappdesign.pinnacleappdesign;

import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements NumberPickerFragment.NoticeDialogListener{

    int memoryIndex = 5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Button firstPaneButton = (Button)findViewById(R.id.first_pane_button1);

        firstPaneButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v){
               DialogFragment newFragment = new NumberPickerFragment();

                Bundle args = new Bundle();

                args.putInt("currentMemoryIndex", memoryIndex);
                newFragment.setArguments(args);
                newFragment.show(getFragmentManager(), "numberPicker");
            }       

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


   public void onDialogPositiveClick(int newMemoryIndex) {
    this.memoryIndex = newMemoryIndex;

    /** Getting the reference of the textview from the main layout */
    TextView tv = (TextView) findViewById(R.id.tv_android);

    /** Setting the selected android version in the textview */
    tv.setText("Your Choice : " + this.memoryIndex);        
   }

}

NumberPickerFragment.java の私のコードは次のとおりです。

 package com.pinnacleappdesign.pinnacleappdesign;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.NumberPicker;
    import android.widget.Toast;

public class NumberPickerFragment extends DialogFragment{

       /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface NoticeDialogListener {
        public void onDialogPositiveClick(int newMemoryIndex);
    }

    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        int currentMemoryIndex = bundle.getInt("currentMemoryIndex");

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

       // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout

        View DialogView = inflater.inflate(R.layout.number_picker, null);

        final NumberPicker np = (NumberPicker)DialogView.findViewById(R.id.numberPicker1);

        np.setMinValue(1);
        np.setMaxValue(100);
        np.setWrapSelectorWheel(false);
        np.setValue(currentMemoryIndex);

        builder.setTitle(R.string.dialog_title)
               .setView(DialogView)
               .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    // User confirmed the dialog
                    int position = np.getValue();           
                    mListener.onDialogPositiveClick(position);  
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
          return builder.create();
    }

}
4

1 に答える 1

0

現在の方法の何が問題になっていますか? これは、Android が開発者に使用することを推奨する方法のようです

内で次のようなこともできますNumberPickerFragment

((MainActivity) getActivity()).yourMethod();

しかし、IMO では、定義済みの Interface メソッドを使用する方がはるかにクリーンで再利用可能です。

于 2013-07-03T18:57:03.537 に答える