jeudi 2 juin 2016

Android: create checkbox via javacode in class fragment

I'm adding list items containing a checkbox to a list in my fragment class like this:

public class CheckboxList extends Fragment {

...

    listViewToDo = (ListView)myView.findViewById(R.id.listViewToDo);

    //creates list of names of the default list items
    arrayListToDo = new ArrayList<String>();
    if(!arrayListToDo.contains("Menu1"))arrayListToDo.add("Menu1");
    if(!arrayListToDo.contains("Menu2"))arrayListToDo.add("Menu2");
    if(!arrayListToDo.contains("Menu3"))arrayListToDo.add("Menu3");

    //sets the checkboxes
    arrayListCheck = new ArrayList<>();
    for(int i = 0; i < arrayListToDo.size(); i++){
        CheckBox cb = new CheckBox(myView.getContext());
        cb.setText(arrayListToDo.get(i));
        arrayListCheck.add(cb);
    }
    arrayAdapterCheck = new ArrayAdapter<CheckBox>(listViewToDo.getContext(),
            android.R.layout.simple_list_item_checked, arrayListCheck);
    listViewToDo.setAdapter(arrayAdapterCheck);
...

I want it to do it this way because the user shall be able to add and delete specific list items. Though that works fine, my checkboxes look like this:

checkboxes displayed incorreclty

Can someone tell me what I'm doing wrong?




Aucun commentaire:

Enregistrer un commentaire