3
How to create several Checkbox's
via programming?
3
How to create several Checkbox's
via programming?
4
To create CheckBox
programmatically just do you this way:
CheckBox meuCheckbox = new CheckBox(getApplicationContext());
meuCheckbox.setText("Programaticamente criado");
To check if it is enabled or disabled you can use the method setOnCheckedChangeListener()
:
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 add the CheckBox
using the method addView()
. 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.
3
Use ListView
. ListView
organizes the items vertically and has an adapter that creates a View
required for each item in the list.
Put 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 internal 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 the 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);
}
Browser other questions tagged java android checkbox
You are not signed in. Login or sign up in order to post.
Each time I create a checkbox I have to call the Student class (I’ve already created) and then call the getview method is this ?
– Antonio_Sistema_de_Informacao
No. The
ListView
calls thegetView
when you need me.– NX1125
It worked, I’m just lost in that Bancodedice.Get Students();
– Antonio_Sistema_de_Informacao
BancodeDados.obterAlunos();
is used to get students to appear in the list. It may be from elsewhere, I used a database as an example. Where do you get the student list?– NX1125