获取以编程方式添加的视图的价值

时间:2017-09-03 16:40:04

标签: android android-viewpager

我在ViewPager下的Fragments中以编程方式添加了RadioButton和Checkbox。以下是我添加RadioButton / Checkbox的方法。

public class TeaserExam extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //get all the checked values here
            }
        }
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {    

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1,questionList.get(position).getId(),
                    questionList.get(position).getQuestion(),questionList.get(position).getQuestionType(),questionList.get(position).getChoices());
        }
    }

    public static class PlaceholderFragment extends Fragment {

        public static PlaceholderFragment newInstance(int number, int questionID, String question, int questionType, List<Choices> choices) {
            PlaceholderFragment fragment = new PlaceholderFragment();

            ArrayList<Integer> choicesID = new ArrayList<>();
            ArrayList<String> choicesLabel = new ArrayList<>();
            for (int i = 0; i < choices.size(); i++){
                choicesID.add(i,choices.get(i).getId());
                choicesLabel.add(i,choices.get(i).getLabel());
            }

            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, number);
            args.putInt("questionID", questionID);
            args.putInt("questionType", questionType);
            args.putInt("choicesSize", choices.size());
            ParcelableChoices parcelableChoices = new ParcelableChoices(choicesID,choicesLabel);
            args.putParcelable("parcelableChoices", parcelableChoices);

            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_teaser_exam, container, false);

            int questionID = getArguments().getInt("questionID");
            int questionType = getArguments().getInt("questionType");
            int choicesSize = getArguments().getInt("choicesSize");
            ParcelableChoices parcelableChoices = getArguments().getParcelable("parcelableChoices");

            LinearLayout ll_choices = (LinearLayout)rootView.findViewById(R.id.ll_choices);

            ArrayList<Integer> choicesIDs = parcelableChoices.getId();
            ArrayList<String> choicesLabels = parcelableChoices.getLabel();

            if (questionType == 2){
                //checkboxes
                for (int i = 0; i < choicesSize; i++){
                    CheckBox myChoices = new CheckBox(getContext());
                    myChoices.setId(choicesIDs.get(i));
                    myChoices.setText(choicesLabels.get(i));
                    myChoices.setLayoutParams(new LinearLayout.LayoutParams(
                                             LinearLayout.LayoutParams.WRAP_CONTENT,
                                             LinearLayout.LayoutParams.WRAP_CONTENT));

                    ll_choices.addView(myChoices);
                }
            }else{

                //radio group
                RadioGroup rg = new RadioGroup(getContext());
                rg.setOrientation(RadioGroup.VERTICAL);
                for (int i = 0; i < choicesSize; i++){
                    RadioButton myChoices = new RadioButton(getContext());
                    myChoices.setId(choicesIDs.get(i));
                    myChoices.setText(choicesLabels.get(i));
                    myChoices.setLayoutParams(new LinearLayout.LayoutParams(
                                             LinearLayout.LayoutParams.WRAP_CONTENT,
                                             LinearLayout.LayoutParams.WRAP_CONTENT));
                    rg.addView(myChoices);
                }
                ll_choices.addView(rg);
            }

            return rootView;
        }
    }
}

问题是现在我不知道如何检索已检查的radiobutton / checkboxes值。当我点击一个只出现在最后一页的按钮时,我希望获得所有选中的值。

由于所有视图都添加到ll_choices中,有没有办法可以获取ll_choices下添加的所有视图的id和值?

2 个答案:

答案 0 :(得分:2)

您问题的直接答案是保留对以编程方式添加的控件的引用。您可以使用成员字段变量而不是局部变量来执行此操作。

由于您要添加动态视图的列表,因此您应该了解ListViewRecyclerView。这些是为添加动态视图而设计的。我还建议您将RadioButtonCheckBox es分成两个单独的片段。然后活动决定显示哪个片段。

答案 1 :(得分:1)

目前,您的变量是片段内onCreate的本地变量。

在fragment类中创建类字段,在onCreate中初始化它们然后它们将在整个片段类中可用