Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

android add checkbox dynamically

The following are my layout xmls. Now we are have a limited number (seven) of check boxes in the layout (listitem.xml). And we are adding it in a linear layout.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#FFFFFF"
   >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="38dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/linearLayout1"
            android:layout_alignLeft="@+id/linearLayout1"
            android:text="TESTER : "
            android:textColor="#000000" />

        <AutoCompleteTextView
            android:id="@+id/tester_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:capitalize="characters" 
            android:textColor="#000000">

            <requestFocus />
        </AutoCompleteTextView>

    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="APPS TO TEST : "
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:textColor="#000000"/>




    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_below="@+id/textView2"
        android:background="#CCCCCC" 
        android:layout_marginBottom="5dp"/>

    <ListView
        android:id="@+id/apps_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/view1" 
        android:dividerHeight="5dp">
    </ListView>

    <Button
        android:id="@+id/refreshBtn"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:text="Refresh" />

</RelativeLayout>

listitem.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000" />

        <Button
            android:id="@+id/launch_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_weight="20"
            android:text="LAUNCH" />
    </LinearLayout>

    <RadioGroup
        android:id="@+id/status_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/pass_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="PASS"
            android:textColor="#000000" />

        <RadioButton
            android:id="@+id/fail_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FAIL"
            android:textColor="#000000" />
    </RadioGroup>

    <LinearLayout
        android:id="@+id/failure_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center"
            android:text="FAILURE REASONS"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000" />

        <LinearLayout
            android:id="@+id/failure_reasonslist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:orientation="vertical" >

            <CheckBox
                android:id="@+id/failure_Sound"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000" />

            <CheckBox
                android:id="@+id/failure_GPS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="#000000" />

            <CheckBox
                android:id="@+id/failure_Display"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="#000000" />

            <CheckBox
                android:id="@+id/failure_Network"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="#000000" />

            <CheckBox
                android:id="@+id/failure_Rotation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="#000000" />

            <CheckBox
                android:id="@+id/failure_Microphone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="#000000" />

            <CheckBox
                android:id="@+id/failure_Others"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="#000000" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lineatlayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DESCRIPTION : "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/description_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:ems="10"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:inputType="textMultiLine"
            android:textColor="#000000" >
        </EditText>
    </LinearLayout>
 <LinearLayout
        android:id="@+id/lineatlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <Button
        android:id="@+id/submit_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="  SUBMIT " />
    <Button
        android:id="@+id/uninstall_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UNINSTALL" />
    </LinearLayout>

    <!-- </RelativeLayout> -->

</LinearLayout>

But as per the new requirement we need to add the check boxes dynamically. Like need to fetch the options from the API call and convert it as check boxes in the view. I spent lots of time in it. Can someone suggest a method to solve this.

Our layout should be like this. In a list view we are displaying employee details and for each employee record the qualification should be loaded from the API call or database.

      [Reviewer Name (Editable)]       
               Employee Lest           
---------------------------------------
Employee1                    [Click Me]

Male (0)          Female ( )
Salary : [5999 (editable)]
Qualification
[X]BA
[ ]BSc
[ ]Matriculation
[ ]Other
[Submit Button]
---------------------------------------
Employee2                    [Click Me]

Male (0)          Female ( )
Salary : [5999 (editable)]
Qualification
[X]BA
[ ]BSc
[ ]Matriculation
[ ]Other
[Submit Button]
---------------------------------------
Employee3                    [Click Me]

Male (0)          Female ( )
Salary : [5999 (editable)]
Qualification
[X]BA
[ ]BSc
[ ]Matriculation
[ ]Other
[Submit Button]
---------------------------------------

Source code

private class ListAdapters extends ArrayAdapter<ApplicationBean> {
    private ArrayList<ApplicationBean> items;
    private int position;

    public ListAdapters(Context context, int textViewResourceId,
            ArrayList<ApplicationBean> mTitleList) {
        super(context, textViewResourceId, mTitleList);
        this.items = mTitleList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        this.position = position;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.applicationlistitem, null);
        }

        final ApplicationBean o = (ApplicationBean) items.get(position);

        if (o != null) {

            txtAppName = (TextView) v.findViewById(R.i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
LinearLayout featuresTable = (LinearLayout) findViewById(R.id.failure_reasonslist);
// use loop 
CheckBox feature1 = new CheckBox(this);
featuresTable.addView(feature1 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...