【问题标题】:is it possible to send data from dialog fragment to fragment related to activity and how?是否可以将数据从对话片段发送到与活动相关的片段以及如何发送?
【发布时间】:2016-06-07 09:20:06
【问题描述】:

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

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

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

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

通过尝试使用 bundle 得到异常 bundle 为 null。

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

这是我的对话片段:

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

接收数据的片段(主):

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

【问题讨论】:

标签: android android-fragments android-dialogfragment


【解决方案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();

【讨论】:

    【解决方案2】:

    1.创建接口:

    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.调用对话框:

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

    【讨论】:

    • 是的,对话框由活动打开,但接收到的数据用于片段(主)。在这里表示我作为页面查看器的主要活动,它由两个片段组成。其中一个片段(主要)需要对话框片段的数据..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多