How to create CheckBox via schedule [closed]

3

How to create multiple Checkbox's via programming?

    
asked by anonymous 23.01.2017 / 02:13

2 answers

3

Use ListView . ListView organizes items vertically and has an adapter that creates a View needed for each item in the list.

Place the ListView where you want it in the layout. For example:

<?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">

    <ListView
        android:id="@+id/lista_presenca"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

Now in the activity class create an inner class that extends ArrayAdapter<Aluno> . Aluno is the student class with your information. The extension will look like the following class:

private class AlunoAdapter extends ArrayAdapter<Aluno> {

    public AlunoAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_1);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Aluno aluno = getItem(position);

        if (convertView == null) {
            convertView = new CheckBox(getContext());
        }

        CheckBox checkBox = (CheckBox) convertView;

        checkBox.setText(aluno.getNome());
        checkBox.setChecked(false);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // salva no banco de dados se o aluno está presente ou não
            }
        });

        return checkBox;
    }
}

And onCreate(Bundle) :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listaPresenca = (ListView) findViewById(R.id.lista_presenca);

    Aluno[] alunos = BancoDeDados.obterAlunos();

    AlunoAdapter adapter = new AlunoAdapter();
    adapter.addAll(alunos);

    listaPresenca.setAdapter(adapter);
}
    
23.01.2017 / 02:54
4

To create CheckBox programmatically, you simply do this:

CheckBox meuCheckbox = new CheckBox(getApplicationContext());
meuCheckbox.setText("Programaticamente criado");

To check whether it is enabled or disabled you can use the setOnCheckedChangeListener() method:

meuCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                 //checkbox habilitado
            } else {
                 //checkbox desabilitado
            }                  
        }
});

For you to add it to your ListView , for example, insert an id and append the CheckBox using the addView() method. See below:

XML

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

</LinearLayout>

Class

ListView list = (LinearLayout) findViewById(R.id.listview);
list.addView(meuCheckbox);

' For more details on CheckBox , see in the documentation .

    
23.01.2017 / 02:20