Example of my Recyclerview Adapter, Delete Action
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("Confirmação")
.setIcon(android.R.drawable.ic_menu_delete)
.setMessage("Tem certeza que deseja excluir este registro?")
.setPositiveButton("Excluir", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
Beneficiary.deleteAllBeneficiary(holder.id);
notifyItemChanged(position);
notifyItemRangeChanged(position, getItemCount());
notifyItemRemoved(position);
beneficiaryList.remove(position);
Toast.makeText(view.getContext(), "Registro Excluido! ", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(view.getContext(), "Erro ao Excluir! ", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancelar", null)
.create()
.show();
}
});
In my Activity to show the amount of registration I’m making
List ben = Beneficiary.findAll();
if (ben.size() > 1){
setTitle("Beneficiários ("+ben.size()+")");
}else{
setTitle("Beneficiário ("+ben.size()+")");
}
And the code inside the Synchronization App, where it calls asynctask
ben = Beneficiary.findAll();
txtCountRegister.setText("Quantidade de registros: " + ben.size());
Down there I have the action button that starts the timing.
btnSync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ben.size() == 0) {
Funcoes.Alert(getBaseContext(), "Nenhum registro para ser sincronizado");
} else {
BeneficiarySync beneficiarySync = new BeneficiarySync(SyncActivity.this);
beneficiarySync.execute();
}
}
});
Method of Ackgroud
@Override
protected String doInBackground(Integer... integers) {
ErrorSync ClearTableError = new ErrorSync();
ClearTableError.DeleteAllRegister();
this.publishProgress("testConnection", "Testando conexão com a Internet");
if (Funcoes.verificaConexao(context)) {
if (VerifyUser()) {
for (int i = 0; i < ben.size(); i++) {
message = "";
int cont = i + 1;
message += "Enviando Registro " + cont + " de " + ben.size() + "\n\n";
Integer beneficiaryIdSuccess = SendBeneficiary(ben.get(i));
beneficiary = Beneficiary.findByIdBeneficiary(ben.get(i).getId());
beneficiary.setIdBeneficiaryWeb(beneficiaryIdSuccess);
beneficiary.updateBeneficiary();
if (beneficiaryIdSuccess > 0) {
if (ben.get(i).getMarital_status().equals("Casado") || ben.get(i).getMarital_status().equals("Amasiado")) {
SendSpouse(ben.get(i).getId(), beneficiaryIdSuccess);
}
sendCompositionFamily(ben.get(i).getId(), beneficiaryIdSuccess);
sendHouseInformation(ben.get(i).getId(), beneficiaryIdSuccess);
SendAttachment(ben.get(i).getId(), beneficiaryIdSuccess);
verifyRegister(ben.get(i).getId(), beneficiaryIdSuccess, i);
}
}
} else {
message = "Dados do Usuário na plataforma estão diferentes do Aplicativo. Verifique!!!";
this.publishProgress("userInvalid", message);
progressDialog.dismiss();
cancel(true);
}
} else {
String msg = "Não foi possivel estabelecer uma conexão. Verifique sua conexão com a internet!!!";
this.publishProgress("internetNo", msg);
progressDialog.dismiss();
cancel(true);
}
List<ErrorSync> errorSyncsList = ErrorSync.findAllError();
if (errorSyncsList.size() > 0) {
this.publishProgress("ok", "Finalizado a Sincronização \n\n Quantidade de Erros: " + errorSyncsList.size());
} else {
this.publishProgress("ok", "Finalizado a Sincronização dos Dados");
}
return "Concluido";
}
In this scenario the records that had errors they remain in the app for fixes, so the textview of Activity has to be updated.
Without the code it is difficult, but in both cases I believe you can pass a 'callback', which you can call from your Recyclerview and Asynctask when it is necessary to update the counter.
– Diniz