I know its a little odd but it will be very useful for my app . I tried setting the adapter with ArrayList and Array of CheckBoxe's in both cases codes compiled okay but it doesn’t executed in android I tried these :
Array Attempt : definitions in the class ,
CheckBox[] listCB ;
CheckBox CB1 , CB2 , CB3 ;
ListView lv ;
overrided onCreate() method :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listCB = new CheckBox[2] ;
CB1 = new CheckBox(null,null);
CB2 = new CheckBox(null,null);
CB3 = new CheckBox(null,null);
CB1.setText("one");
CB2.setText("two");
CB3.setText("three");
listCB[0] = CB1 ;
listCB[1] = CB2 ;
listCB[2] = CB3 ;
lv = (ListView) findViewById(R.id.listView) ;
lv.setAdapter(new ArrayAdapter<CheckBox>(this , R.layout.listcb , listCB));
}
ArrayList Attempt : definitions in the class :
ArrayList<CheckBox> listCB ;
CheckBox CB1 , CB2 , CB3 ;
ListView lv ;
overrided onCreate() method :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listCB = new ArrayList<CheckBox>() ;
CB1 = new CheckBox(null,null);
CB2 = new CheckBox(null,null);
CB3 = new CheckBox(null,null);
CB1.setText("one");
CB2.setText("two");
CB3.setText("three");
listCB.add(CB1) ;
listCB.add(CB2) ;
listCB.add(CB3) ;
lv = (ListView) findViewById(R.id.listView) ;
lv.setAdapter(new ArrayAdapter<CheckBox>(this , R.layout.listcb , listCB));
}
and the layout which referred to :
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/checkBox"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textColor="#ffffffff"
android:clickable="true">
</CheckBox>
Both of them failed !