Auto update do RecyclerView

  • Respostas:0
  • AbertoNão stickySem resposta
  • Posts no fórum: 1

13/09/2017, 23:04:09 via Web

Olá, eu tenho um Frament que consome uma API (JSON) e me retorna uma lista em um RecyclerView, teria alguma forma de essa lista atualizar automaticamente quando tiver novos registros incluídos por outro usuário ou sistema?

public class AbertosFragment extends Fragment {

private static final String TAG = "Abertos";

ProgressDialog progressDialog;
RecyclerView recyclerView;
TicketAdapter ticketAdapter;
List<Ticket> listaDados;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_abertos, container, false);

    buscaTickets();

    return view;
}

public void buscaTickets() {

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setTitle("Buscando informações...");
    progressDialog.show();

    TicketService ticketService = Endpoint.createService(TicketService.class);

    Call<List<Ticket>> call  = ticketService.buscaPorFiltroeExpandindo(
            Endpoint.TOKEN,
            "id,type,subject,category,urgency,status,baseStatus,justification",
            "status eq 'Aguardando' and justification eq 'Retorno do cliente'",
            "id desc",
            "createdBy,owner,clients($expand=organization)");

    call.enqueue(new Callback<List<Ticket>>() {
        @Override
        public void onResponse(@NonNull Call<List<Ticket>> call, @NonNull Response<List<Ticket>> response) {
            if (!response.isSuccessful()) {
                Log.i("TAG", "Erro response: " + response.code());
            } else {
                listaDados = response.body();

                ticketAdapter = new TicketAdapter(listaDados);
                ticketAdapter.notifyDataSetChanged();
                recyclerView = (RecyclerView) getView().findViewById(R.id.listaTicketsAbertos);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(ticketAdapter);

            }

            progressDialog.dismiss();
        }

        @Override
        public void onFailure(@NonNull Call<List<Ticket>> call, @NonNull Throwable t) {
            Log.e(TAG, "Erro: " + t.getMessage());
        }
    });

}

}

Responder

Artigos recomendados