简体   繁体   中英

Adding checkboxes dynamically to the fragment - checkboxes are not visible

I'm trying to add checkbox to the fragment which looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/addProductButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Add Product" />


</LinearLayout>

Then I create checkBox and try to add them to the fragment

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

            LinearLayout ll = new LinearLayout(getActivity());
            ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            ll.setOrientation(LinearLayout.VERTICAL);
            for (int i = 0; i < 20; i++) {
                CheckBox cb = new CheckBox(rootView.getContext());
                ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                cb.setText("check");
                ll.addView(cb);
            }
            return rootView;
        }
    }

But the checkboxes are not visible.

在此处输入图片说明

you can not add the same instance of CheckBox 20times. You need to create a new one at every iteration. Move

inside the for loop

for (int i = 0; i < 20; i++) {
     CheckBox cb = new CheckBox(rootView.getContext());
     ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     cb.setText("check");
     ll.addView(cb);
}

add one id to your linearLayout like:

 android:id="@+id/linearLayout"

then in your code you have:

 LinearLayout ll = rootView.findViewById(R.id.linearLayout);
 // other code

one another way that must work is create one layout and add view and this linearLayout to that

Change Your onCreateView() method like this..no need of findViewById() or any others

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

    // LinearLayout ll = new LinearLayout(getActivity());
    rootView.setOrientation(LinearLayout.VERTICAL);
    for (int i = 0; i < 20; i++) {
        CheckBox cb = new CheckBox(rootView.getContext());
        rootView.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        cb.setText("check");
        rootView.addView(cb);
    }
    return rootView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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