1
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, not to lose the data from it, because when I roll my list view my checkboxes uncheck 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;
}
}
Why that code pile
qtdePaes.setALGMACOISA
repeated in both ifs and Es? You can move these blocks to after the if-lse and leave inside them only what really is different between them.– Márcio Oliveira
I modified my project, it was very messy, @Márciooliveira. I followed your recommendations. it was less code, and working.
– Luciano Pedrosa