0
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:
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 type the student’s name, click on the "add" button and voilà, the name is added to the bank and the list with the items appears below Edittext (registration). For this, I used a dynamic list, in which it starts empty and the user increments.
Now I want to make sure that every time the user goes to add one more 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 don’t really understand what I should do/add to the code.
Are you using Sqlite to record the correct records !? You want to generate a listview, with these records, and next to each, have a checkbox to select them and after this selection, run a routine to with these selected records, this is it ?
– rbz
You need to make an Adapter and a layout for the list item, in which you will put the checkbox and a textview for example. https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
– AndersonCanteiro