如何从Fragment中的不同视图中收集值?

时间:2015-05-24 12:01:56

标签: android

我想从Fragment的所有视图中收集所选单选按钮中的数据并将其发布到服务器,我该如何收集数据?至少有人可以帮我吐司吗?

注意:此时我已经在一个屏幕上执行此操作,但我想要所有屏幕的集体数据。

吐司

Button myButton = (Button) layoutView.findViewById(R.id.findSelected);
            myButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    StringBuffer responseText = new StringBuffer();
                    responseText.append("");

                    // Get selected radiobuttons
                    if (radioGroup1.getCheckedRadioButtonId() != -1) {
                        text1 = btn.getText().toString();
                        Log.d("Button", "Text 1 : " + text1);
                    }

                    if (radioGroup2.getCheckedRadioButtonId() != -1) {
                        text2 = btn2.getText().toString();
                        Log.d("Button", "Text 2 : " + text2);
                    }


                    Toast.makeText(
                            thiscontext,
                            "Data Posting : APPLICATION : "
                                    + text1 + " \nDEVICE : " + text2,
                            Toast.LENGTH_LONG).show();


                }
            });

完整代码:

LayoutFragment.java

public class LayoutFragment extends Fragment {
    int fragVal;
    private String[] application = { "Country1", "Country2", "Country3", "Country4", "Country5", "Country6", "Country7", "Country8" };                     
    private String[] device = { "Country9", "Country10", "Country11", "Country12", "Country13", "Country14", "Country15", "Country16" }; 
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    RadioButton button1;
    RadioButton button2;
    Button selectall;
    Context thiscontext;

    static LayoutFragment init(int val) {
        LayoutFragment truitonFrag = new LayoutFragment();
        // Supply val input as an argument.
        Bundle args = new Bundle();
        args.putInt("val", val);
        truitonFrag.setArguments(args);
        return truitonFrag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        thiscontext = container.getContext();
        View layoutView = inflater.inflate(R.layout.activity_main, container, false);

        Button myButton = (Button) layoutView.findViewById(R.id.findSelected);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                StringBuffer responseText = new StringBuffer();
                responseText.append("");

                // Get selected radiobuttons
                if (radioGroup1.getCheckedRadioButtonId() != -1) {
                    text1 = btn.getText().toString();
                    Log.d("Button", "Text 1 : " + text1);
                }

                if (radioGroup2.getCheckedRadioButtonId() != -1) {
                    text2 = btn2.getText().toString();
                    Log.d("Button", "Text 2 : " + text2);
                }


                Toast.makeText(
                        thiscontext,
                        "Data Posting : APPLICATION : "
                                + text1 + " \nDEVICE : " + text2,
                        Toast.LENGTH_LONG).show();


            }
        });


        //Draw Radiobuttons

        radioGroup1 = (RadioGroup) layoutView.findViewById(R.id.radio1);
        radioGroup2 = (RadioGroup) layoutView.findViewById(R.id.radio2);

        ViewGroup hourButtonLayout = (ViewGroup) layoutView.findViewById(R.id.radio1);
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(thiscontext);
            button1.setId(i);
            button1.setText(application[i]);
            hourButtonLayout.addView(button1);

            radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup2,
                                int checkedId2) {
                            for (int i = 0; i < mRadioGroup2.getChildCount(); i++) {
                                btn = (RadioButton) mRadioGroup2.getChildAt(i);
                                int t = mRadioGroup2.getId();
                                System.out.println(t);

                                if (btn.getId() == checkedId2) {
                                    text1 = btn.getText().toString();
                                    Toast.makeText(thiscontext,
                                            "You selected : " + text1,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) layoutView.findViewById(R.id.radio2); 
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(thiscontext);
            button2.setId(i);
            button2.setText(device[i]);
            hourButtonLayout2.addView(button2);

            radioGroup2
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup,
                                int checkedId) {
                            for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
                                btn2 = (RadioButton) mRadioGroup.getChildAt(i);
                                int t = mRadioGroup.getId();
                                System.out.println(t);

                                if (btn2.getId() == checkedId) {
                                    text2 = btn2.getText().toString();
                                    Toast.makeText(thiscontext,
                                            "You selected : " + text2,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }



        return layoutView;
    }
}

1 个答案:

答案 0 :(得分:0)

在每个片段中添加一个公共方法,例如

public HashMap<String, Boolean> getRadiosStatus{
    //TODO put every radio button's status to hashmap and return it.
}

然后,在活动中调用此方法。您可以通过调用所有相同的方法从所有片段中获取所有状态。