是否可以将对话框片段中的数据发送到与活动相关的片段以及如何?

时间:2016-06-07 09:20:07

标签: android android-fragments android-dialogfragment

在我的应用程序中,我有一个对话框片段,在启动MainActivity之前首先打开。当在对话框片段中输入一些数据并单击确认时,我需要向片段(Main)发送一些值(表示双精度值)。

我的主要活动包括视图寻呼机,它有两个片段。一个是片段(主要)..我必须将数据发送到我的对话框片段到片段(主要)

片段(Main)通过viewpager附加到MainActivity。 我已经尝试过使用Intents和bundle将值发送到fragment(Main)。 引用网站在片段之间传递数据。

虽然通过使用Intents它显示异常无法显式,你在android Manifest中声明了

通过使用bundle尝试获取异常包是空的。

从这两个例外我得到的是可以发送数据.. 任何人都可以提供帮助并提出建议。

这是我的对话框片段:

public class InputFragment extends DialogFragment {



public InputFragment() {
    // Required empty public constructor
}


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   View view = inflater.inflate(R.layout.input, container, false);
    final EditText ed = (EditText)view.findViewById(R.id.editText);
    final RadioButton rb1 = (RadioButton)view.findViewById(R.id.radioButton1);
    final RadioButton rb2 = (RadioButton)view.findViewById(R.id.radioButton2);
    final Button b1 = (Button)view.findViewById(R.id.cancel);
    final Button b2 = (Button)view.findViewById(R.id.confirm);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!ed.getText().toString().equals("")) {
                if (rb1.isChecked()) {
                    int input = Integer.valueOf(ed.getText().toString());
                    double out = input / 24.0;
                    out = Double.parseDouble(new DecimalFormat("#.0").format(out));
                    Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
                    m.putExtra("res", out);
                    getActivity().startActivity(m);

                } else if (rb2.isChecked()) {
                    int input = Integer.valueOf(ed.getText().toString());
                    double out = ((input * 2 / 3) * 0.029574);
                    out = Double.parseDouble(new DecimalFormat("#.0").format(out));
                    Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
                    m.putExtra("res", out);
                    getActivity().startActivity(m);
                }
            }
        }
    });
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    return view;
}

接收数据的片段(Main):

public class MainScreen extends Fragment {

double ram;
TextView gls;
MainScreen mainScreen;

public MainScreen() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.main_screen, container, false);
    mainScreen = this;
    TextView textView = (TextView)view.findViewById(R.id.res);
    Intent r = getActivity().getIntent();
    double res = r.getDoubleExtra("res", 0);
    String result = Double.toString(res);
    textView.setText(result);


    ram = Double.parseDouble(textView.getText().toString());
    gls = (TextView) view.findViewById(R.id.glasses);
    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DiaFragment df = new DiaFragment();
            df.setCallingFragment(mainScreen);
            df.show(getFragmentManager(),"Frag");
        }
    });


    return view;
}

上面我尝试了意图,

使用bundle: 在我的片段(主要)

Bundle mbundle = mainScreen.getArguments();
    double res = mbundle.getDouble("res",0);
    String result = Double.toString(res);
    textView.setText(result);

对话框片段:

InputFragment inputFragment = new InputFragment();
                        Bundle bundle = new Bundle();
                        bundle.putDouble("res",out);
                        inputFragment.setArguments(bundle);

2 个答案:

答案 0 :(得分:1)

1.create interface:

public interface OnReceivedData {
    public void onDataReceived(Objects objects);
}

2.创建对话框:

public class Dialog extends DialogFragment {
    private OnReceivedData mData;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mData=(OnReceivedData) activity;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // create dialog
        senDataToActivity();
    }

    private void senDataToActivity() {
        mData.onDataReceived("your object");
    }
}

3.call对话框:

public class MainActivity extends AppCompatActivity implements OnReceivedData  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    private void opneDialog(){
        //
    }
    @Override
    public void onDataReceived(Objects objects) {
        // receded data
        // do something
    }
}

答案 1 :(得分:1)

你无法调用这样的片段:

Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);

取而代之的是:

MainScreen fragment = new MainScreen();
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); 
ft.replace(id, fragment , "MainScreen");
ft.addToBackStack("");
ft.commit();