Update totalizer in actionbar when deleting record

Asked

Viewed 24 times

0

Good evening, you guys, I’m developing a basic app, and came across some problems updating registrations totaliser:

  1. I have an Activity containing a Recyclerview listing the data stored in the database. Where in actionbar I added a counter to inform the amount of registered records. My problem is that by deleting one of these records I am not able to find a way to update this counter in actionbar, if I leave Activity and return correct recalculate.

  2. The registered data will be sent to a webservice later, in the Synchronism Activity has a textview informing the amount of records that will be synchronized. In my sending process I use an asynctask for this, if the sending of the registration in question receive an "OK" from the webservice the same is deleted from the app. At the end of the synchronization would be only the records with some error or failure happens that this counter is also not able to update to the end of the process. I need to leave Activity and return to update.

If anyone can help me, I’d appreciate it.

  • 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.

1 answer

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.