This is a code from my adapter, in it I have a textview and 4 chekboxes. The problem is that I believe I have to save the status of my checkboxes, so as not to lose their data, because when I roll my list view my checkboxes uncheck them alone.
public class ListaFuncionarioAdapter extends BaseAdapter {
/*
private Activity activity;
private List<MeuItem> itens;
public ListaFuncionarioAdapter(Activity activity, List<MeuItem> itens){
this.activity = activity;
this.itens = itens;
}
*/
private List<MeuItem> itens;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
CheckBox chkSalManha, chkDoceManha, chkSalTarde, chkDoceTarde;
private Context context;
private List<QtdePaes> listQtdePaes = new ArrayList<QtdePaes>();
private ArrayAdapter<QtdePaes> arrayAdapterQtdePaes;
QtdePaes qtdePaesSelecionado;
private List<Funcionario> listFuncionario;
final QtdePaes qtdePaes = new QtdePaes();
int salmanha = 0;
int saltarde = 0;
int docemanha = 0;
int docetarde = 0;
public ListaFuncionarioAdapter(Context context, List<Funcionario> listFuncionario) {
this.context = context;
this.listFuncionario = listFuncionario;
}
private void inicializarFirebase() {
FirebaseApp.initializeApp(context);
firebaseDatabase = FirebaseDatabase.getInstance();
// firebaseDatabase.setPersistenceEnabled(true);
databaseReference = firebaseDatabase.getReference();
//DatabaseReference novaReference = firebaseDatabase.getReference();
// novaReference = databaseReference.child("minh");
}
@Override
public int getCount() {
return listFuncionario.size();
}
@Override
public Object getItem(int position) {
return listFuncionario.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= View.inflate(context, R.layout.item_funcionario, null);
chkSalManha = (CheckBox)v.findViewById(R.id.chkSalManha);
chkDoceManha = (CheckBox)v.findViewById(R.id.chkDoceManha);
chkSalTarde = (CheckBox)v.findViewById(R.id.chkSalTarde);
chkDoceTarde = (CheckBox)v.findViewById(R.id.chkDoceTarde);
TextView txtNome = (TextView)v.findViewById(R.id.txtNome);
inicializarFirebase();
txtNome.setText(String.valueOf(listFuncionario.get(position).getNome()));
//MeuItem item = itens.get(position);
//chkSalManha.setTag(item);
//chkSalManha.setChecked(item.foiMarcado());
v.setTag(listFuncionario.get(position).getUid());
chkSalManha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
salmanha += 1;
String estado = (String) chkSalManha.getTag();
// qtdePaes.setUid(UUID.randomUUID().toString());
qtdePaes.setQtdeSalManha(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
//databaseReference.child("QtdePaes").child(qtdePaes.getUid()).setValue(qtdePaes);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
if(!isChecked)
{
salmanha -= 1;
//qtdePaes.setUid(qtdePaesSelecionado.getUid());
qtdePaes.setQtdeSalManha(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
}
});
chkSalTarde.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
saltarde += 1;
//qtdePaes.setUid(UUID.randomUUID().toString());
qtdePaes.setQtdeSalTarde(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
//databaseReference.child("QtdePaes").child(qtdePaes.getUid()).setValue(qtdePaes);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
if(!isChecked)
{
saltarde -= 1;
//qtdePaes.setUid(qtdePaesSelecionado.getUid());
qtdePaes.setQtdeSalTarde(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
}
});
chkDoceManha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
docemanha += 1;
//qtdePaes.setUid(UUID.randomUUID().toString());
qtdePaes.setQtdeSalTarde(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
//databaseReference.child("QtdePaes").child(qtdePaes.getUid()).setValue(qtdePaes);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
if(!isChecked)
{
docemanha -= 1;
//qtdePaes.setUid(qtdePaesSelecionado.getUid());
qtdePaes.setQtdeSalTarde(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
}
});
chkDoceTarde.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
docetarde += 1;
// qtdePaes.setUid(UUID.randomUUID().toString());
qtdePaes.setQtdeSalTarde(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
//databaseReference.child("QtdePaes").child(qtdePaes.getUid()).setValue(qtdePaes);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
if(!isChecked)
{
docetarde -= 1;
//qtdePaes.setUid(qtdePaesSelecionado.getUid());
qtdePaes.setQtdeSalTarde(salmanha);
qtdePaes.setQtdeDoceManha(docemanha);
qtdePaes.setQtdeSalTarde(saltarde);
qtdePaes.setQtdeDoceTarde(docetarde);
databaseReference.child("QtdePaes").child("6fd2aede-e00f-48d8-b6cb-f1498e23e8e8").setValue(qtdePaes);
}
}
});
return v;
}
}