简体   繁体   中英

Value From DialogFragment to fragment is not displaying

My Requirement:

I have Mainfragment class, from where I am displaying dialogFragment to display seekbar .. after adjust seekbar value,when click OK button, that value from dialogFragment will display in MainFragment textview..

Here I have done below code.

MainFragment class:

public class MainFragment extends Fragment implements OnClickListener,OnDisplayListener {

    static TextView displayDuration;
    TextView  setDuration;

    public MainFragment() {
        }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_one, container, false);
        displayDuration = (TextView) v.findViewById(R.id.displayDuration);
        setDuration = (TextView) v.findViewById(R.id.setDuration);
        setDuration.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
            DialogFragment durationPicker = new DurationFragment();
            durationPicker.show(getActivity().getFragmentManager(), "dialog");

            }

        });
        return v;
    }



    @Override
    public void ondurationSubmit(String duration) {
        // Do stuff
        displayDuration.setText(duration);
    }
}

here , OnDisplayListener Interface :

public interface OnDisplayListener {
    public void ondurationSubmit(String duration);
}

Now DurationFragment fragment is:

public static class DurationFragment extends DialogFragment implements OnSeekBarChangeListener {

    OnDisplayListener callback;
    TextView vsProgress, vs_reverseProgress = null;
    TextView levelTxt=null;

    static DurationFragment newInstance() {
    return new DurationFragment();
    }
     @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View content = inflater.inflate(R.layout.dialog_setting2, null);
    levelTxt = (TextView) content.findViewById(R.id.textView);
    SeekBar levelSeek = (SeekBar) content.findViewById(R.id.seek_bar_voice_volume);
    levelSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        // change to progress
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        levelTxt.setText(Integer.toString(progress) + " min");
        }
               @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
               @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
        });

        builder.setView(content);
        builder.setMessage("Your Time")
        // Positive button
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // add value to the interface method
        callback.ondurationSubmit(levelTxt.getText().toString());
        }
        });

        return builder.create();
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {


        }
    }

Now, From the MainFragment When It displays dialogFragment, After select seek bar value when click Ok button then the app stops..

saying error found :

callback.ondurationSubmit(levelTxt.getText().toString());

in this line..why the value is not displaying in mainFragment??

In DurationFragment add a new constructor:

public DurationFragment( OnDisplayListener callback) {
    this.callback = callback;
}

In MainFragment, in onClick do this:

DialogFragment durationPicker = new DurationFragment(MainFragment.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM