Recycler view com async task

Asked

Viewed 198 times

0

I have the class of my Activity

public class Convenio_Conselho_Listar_TL extends AppCompatActivity {

public List<Convenio_Conselho_Grid_BD> oConvenios;
RecyclerView mRecyclerView;
private LineAdapter mAdapter;


public void setoConvenios(List<Convenio_Conselho_Grid_BD> convenios) {
    this.oConvenios = convenios;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_convenio_conselho_listar_tl);
    getSupportActionBar().setTitle(getString(R.string.Convenio_Conselho));


    try {

        mAdapter = new LineAdapter((ArrayList) oConvenios);
        mRecyclerView = findViewById(R.id.rcv_Convenio_Conselho);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(layoutManager);

        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addItemDecoration(
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        new getConvenios(this).execute();
        mAdapter.notifyDataSetChanged();

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Não foi possível recuperar os convênios.", Toast.LENGTH_LONG).show();
        Log.d("Erro", e.toString());
    }
    mRecyclerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int itemPosition = mRecyclerView.getChildLayoutPosition(view);
            String item = String.valueOf(oConvenios.get(itemPosition));
            Snackbar.make(view, "Clicou no " + item, Snackbar.LENGTH_LONG)
                    .setAction("Email", null).show();
        }
    });
}


class getConvenios extends AsyncTask<Void, Void, List<Convenio_Conselho_Grid_BD>> {

    List<Convenio_Conselho_Grid_BD> oLista = new ArrayList<>();
    private Exception exception;
    private Convenio_Conselho_Listar_TL Atividade;
    private ProgressDialog dialog;


    public getConvenios(Convenio_Conselho_Listar_TL atividade) {
        this.Atividade = atividade;
    }

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(Convenio_Conselho_Listar_TL.this);
        dialog.setMessage("Carregando...");
        dialog.setTitle("Carregando os Convênios");
        dialog.setCancelable(false);
        dialog.show();

    }

    @Override
    protected List<Convenio_Conselho_Grid_BD> doInBackground(Void... voids) {
        {
            try {
                SoapObject ob = SG_Smartphone_NG.Convenio_Conselho().Pesquisar_Convenios(1);

                for (int i = 0; i < ob.getPropertyCount(); i++) {
                    SoapObject ob2 = (SoapObject) ob.getProperty(i);

                    for (int j = 0; j < ob2.getPropertyCount(); j++) {
                        SoapObject ob3 = (SoapObject) ob2.getProperty(j);
                        Convenio_Conselho_Grid_BD oConvenio = new Convenio_Conselho_Grid_BD();

                        oConvenio.setCodigo(Short.parseShort(ob3.getProperty(0).toString()));
                        oConvenio.setCodigo_Entidade(Short.parseShort(ob3.getProperty(1).toString()));
                        oConvenio.setCodigo_Convenio_Conselho_Entidade(Short.parseShort(ob3.getProperty(2).toString()));
                        oConvenio.setNome(ob3.getProperty(3).toString());
                        oConvenio.setDescricao(ob3.getProperty(4).toString());
                        byte[] b = (ob3.getProperty(5).toString()).getBytes();
                        oConvenio.setImagem_Logo_Convenio(b);
                        oConvenio.setE_Ativo(Boolean.parseBoolean(ob3.getProperty(6).toString()));

                        //String oData  =  (ob3.getProperty(7).toString()).substring(0,10);

                        DateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");
                        DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);

                        String inputText = ob3.getProperty(7).toString();
                        Date date = inputFormat.parse(inputText);
                        String outputText = outputFormat.format(date);
                        Date oData = outputFormat.parse(outputText);
                        oConvenio.setData_Atualizacao(oData);

                        oLista.add(oConvenio);
                    }
                    Atividade.setoConvenios(oLista);
                }
            } catch (Exception e) {
                this.exception = e;
            }
            return oLista;
        }
    }

    @Override
    protected void onPostExecute(List<Convenio_Conselho_Grid_BD> convenio_conselho_grid_bds) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

}

The way it looks it shows the dialog but doesn’t update the recyclerview with the data. If I change to new getConvenios(this).execute().get(); it shows the data but does not show the dialog, how to solve?

1 answer

0

Pass the line

mAdapter.notifyDataSetChanged();

into the method onPostExecute()

@Override
protected void onPostExecute(List<Convenio_Conselho_Grid_BD> convenio_conselho_grid_bds) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    mAdapter.notifyDataSetChanged();
}
  • so shows the dialog, loads the data but does not display in recyclerview

  • If it works with getConvenios(this).execute().get(); then it has to work as I say in the answer.

  • should but does not work

Browser other questions tagged

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