Not sure how to dynamically add checkboxes in 2 columns

I am trying to display information from a database as checkboxes. Right now it works fine, but places one column of checkboxes on the screen. What I want to do is have it split into 2.

What it Does Now:
item1
item2
item3
item4
item5

What I Want:
item1 item2
item3 item4
item5

Preferably the new 2 column list would be evenly sized, each getting 50% of the screen, even if the text assigned is different lengths.

I have searched a lot, and tried like 3 different things. Havn't found anything that works. Below is my code as it is (creating one column).

private void listStudents()
{    
    st = (LinearLayout)findViewById(R.id.studTable);
    ml = (RelativeLayout)findViewById(R.id.main);

    ArrayList<ArrayList<String>> studentlst = new ArrayList<ArrayList<String>>();
    studentlst = db.getAllStudentsRowsAsArrays();
    final int sllen = studentlst.size();
    final String student[][] = new String[sllen][3];

    CheckBox cb[] = new CheckBox[sllen];
    TextView pemail[] = new TextView[sllen];
    int num = st.getChildCount();
    if(num != 0) st.removeAllViews();
    String tsl = sllen + "";
    nos.setText(tsl);
    for(int x=0; x < sllen; x++)
    {
        /************************
         *   student[x][case]   *
         *     case options     *
         *        0 = id        *
         *       1 = name       *
         *       2 = email      *
         ************************/
        String curstudent = studentlst.get(x).toString();
        student[x][0] = curstudent.substring(1,curstudent.indexOf(","));
        student[x][1] = curstudent.substring(curstudent.indexOf(" ")+1,curstudent.lastIndexOf(","));
        student[x][2] = curstudent.substring(curstudent.lastIndexOf(" ")+1, curstudent.length() - 1);
    } 
    Arrays.sort(student, new Comparator<String[]>() { 
        @Override 
        public int compare(String[] entry1, String[] entry2) { 
            String name1 = entry1[1]; 
            String name2 = entry2[1]; 
            return name1.compareTo(name2); 
        } 
    });     
    for(int x=0;x<sllen;x++)
    {
      cb[x] = new CheckBox(this);
      cb[x].setId(x+100);
      cb[x].setText(student[x][1]);
      pemail[x] = new TextView(this);
      pemail[x].setText(student[x][2]);
      pemail[x].setId(x+1000);
      pemail[x].setVisibility(View.INVISIBLE);
        st.addView(cb[x],x);
        ml.addView(pemail[x],x);
    }
}

XML File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/lblmainselclass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="82dp"
    android:layout_marginTop="7dp"
    android:text="@string/lblmainselectclass" />

<Spinner
    android:id="@+id/mainclassspinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/lblmainselclass" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_below="@+id/lblmainselstudents"
    android:layout_centerHorizontal="true" >

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

<TextView
    android:id="@+id/lblmainselstudents"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/mainclassspinner"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:text="@string/lblselectstud" />

<Button
    android:id="@+id/btnsend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/btnlblsend" />

<Spinner
    android:id="@+id/mainresponsespinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnsend"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="30dp" />

<TextView
    android:id="@+id/mainnumofstudents"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/invis"
    android:visibility="invisible" />

<TextView
    android:id="@+id/classselected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/invis"
    android:visibility="invisible" />

<TextView
    android:id="@+id/responseselected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/invis"
    android:visibility="invisible" />

<TextView
    android:id="@+id/numchecked"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/invis"
    android:visibility="invisible" />

Upvotes: 1

Views: 2013

Answers (3)

S Fitz
S Fitz

Reputation: 1094

Old question, but I found myself here so I'll go ahead and answer it. There are a couple ways, but here's how I did it.

First, get your data and populate an ArrayList of Checkboxes:

ArrayList<CheckBox> checkboxes = new ArrayList<CheckBox>();
for (String s : dataArray) {
    CheckBox cBox = new CheckBox(myContext); //or inflate it from xml if you have a defined style
    cBox.setText(s);
    checkboxes.add(cBox);
}

Then create a class that extends BaseAdapter along with a supporting static class. We will bind the data to the adapter assuming your CheckBox ArrayList is an instance variable. If not, you can create a constructor in the BaseAdapter class access checkboxes that way.

private class CheckboxGridAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return checkboxes.size();
    }

    @Override
    public Object getItem(int pos) {
        return checkboxes.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        final ViewPlaceHolder holder;
        final CheckBox item = (CheckBox) getItem(pos);

        if (convertView == null) {
        holder = new ViewPlaceHolder();
        holder.cb = (CheckBox) item;
        convertView = holder.cb;

        convertView.setTag(holder);
        } else {
        holder = (ViewPlaceHolder) convertView.getTag();
        }

        return convertView;
    }
}

static class ViewPlaceHolder {
    CheckBox cb;
}

Finally, back where you created the ArrayList of Checkboxes, create a GridView (either programmatically or by inflating an xml resource), then attach the BaseAdapter. Don't forget to set the number of columns, that's the whole point of this post after all.

GridView grid = new GridView(myContext);
grid.setNumColumns(2);
grid.setAdapter(new CheckboxGridAdapter());

Upvotes: 2

Maria Neumayer
Maria Neumayer

Reputation: 3357

You should use a GridView instead of your ScrollView. This way you can dynamically create your items and have two (or more columns)

Upvotes: 0

BrainCrash
BrainCrash

Reputation: 13182

You should use a custom ListView and a BaseAdapter for efficency if you have many entries.

You can find good examples to start with in the SDK ApiDemos (android-sdk/samples/android-x/ApiDemos).

Upvotes: 0

Related Questions