简体   繁体   English

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

[英]is it possible to send data from dialog fragment to fragment related to activity and how?

In my application I have a dialog fragment which opens first before launching of MainActivity. 在我的应用程序中,我有一个对话框片段,该片段在启动MainActivity之前首先打开。 When some data is entered in that dialog fragment and click confirmed I need to send some value (means a double value) to a fragment(Main). 当在该对话框片段中输入一些数据并单击确认时,我需要向片段(Main)发送一些值(表示双精度值)。

My main activity consists of view pager which has two fragments. 我的主要活动包括具有两个片段的视图寻呼机。 one is fragment(Main).. i have to send the data to my dialog fragment to the fragment(main) 一个是fragment(Main)..我必须将数据发送到我的对话框中fragment(main)

The fragment(Main) is attached to MainActivity by viewpager. 片段(Main)通过viewpager附加到MainActivity。 I have tried by sending the value to fragment(Main) by using Intents and bundle. 我已经尝试通过使用Intent和bundle将值发送到fragment(Main)。 referred the sites to pass data between fragments. 引用站点在片段之间传递数据。

while by using Intents it showing the exception unable to explicit have you declared in android Manifest 而通过使用Intents可以显示无法明确显示的异常,是否已在android Manifest中声明了

while by trying with bundle it getting exception bundle is null. 而尝试使用bundle则获取异常bundle为null。

from these two exceptions I have got is it possible to send the data.. can any body help and give suggestion. 从这两个例外我有可能发送数据..任何机构可以帮助和提出建议。

here is my dialog fragment: 这是我的对话框片段:

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

fragment that receive data (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;
}

Above I tried by intent, 以上我是有意尝试的,

by using bundle: in my fragment(Main) 通过使用bundle:在我的片段中(Main)

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

dialog fragment: 对话片段:

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

1.create interface : 1.创建界面:

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

2.create Dialog : 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 dialog : 3.通话对话框

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

You cannot call a fragment like this: 您不能调用这样的片段:

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

Instead this: 相反,这是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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