How to add checkboxes to items in a dynamic list? I'm developing a school attendance app and this is my class to add students to:
public class AddAluno extends AppCompatActivity {
BancoAlunos bancoAlunos;
EditText digitarAluno;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addaluno);
final ListView listV = findViewById(R.id.list);
digitarAluno = findViewById(R.id.digitaraluno);
ImageButton addEstudante = findViewById(R.id.addestudante);
bancoAlunos = new BancoAlunos(this);
//toolbar com seta para voltar para a tela inicial
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
//lista de alunos
ArrayList<String> theList = new ArrayList<>();
Cursor data = bancoAlunos.getListContents();
//se o banco de alunos estiver vazio, retornar msg. Se nao, mostrar lista
if (data.getCount() == 0) {
Toast.makeText(this, "Este grupo está vazio! :(", Toast.LENGTH_LONG).show();
} else {
while (data.moveToNext()) {
theList.add(data.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
listV.setAdapter(listAdapter);
}
}
//adicionar nome à lista quando o botao for clicado
addEstudante.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newEntry = digitarAluno.getText().toString();
if (digitarAluno.length() != 0) {
AddAlunos(newEntry);
digitarAluno.setText("");
} else {
Toast.makeText(AddAluno.this, "Digite o nome do aluno", Toast.LENGTH_LONG).show();
}
}
});
}
//add nome ao banco
public void AddAlunos(String newEntry) {
boolean addAlunos = bancoAlunos.addData(newEntry);
if (addAlunos) {
Toast.makeText(this, "Aluno adicionado com sucesso!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Algo deu errado! :( Tente novamente", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
}
return true;
}
}
Basically the user types the student's name, clicks the "add" button and voila, the name is added to the database, and the list with the items appears below the EditText. For this, I used a dynamic list, in which it starts empty and the user increases.
Now I want to make every time the user adds another item to the list, it already appears with a checkbox on the side. Does anyone know how to solve it? I've looked in some places, but I do not understand much what I should do / add to the code.