Setar Adapter inside a Thread

Asked

Viewed 71 times

0

I’m trying to list in one RecyclerView in the fragment, but the return of the list is in a thread, I don’t know if I’m doing something wrong but it doesn’t show any record.

No logcat returns no error, and the list is filled in when I use Debug.

Follows method with the Adapter being set in and passing the list:

public void ListarArquivosdoFTP() {

        new Thread() {
            @Override
            public void run() {
                try {
                    Recibo recibo = null;
                    classe_FTP ClienteFTP = new classe_FTP();
                    ClienteFTP.Conectar("ftp.meuhost.com.br", "usuario", "senha", 21);
                    FTPFile[] arquivos = ClienteFTP.Dir("/caminho");
                    if (arquivos != null) {
                        int length = arquivos.length;
                        for (int i = 0; i < length; ++i) {
                            FTPFile f = arquivos[i];
                            if (f.isFile()) {
                                recibo = new Recibo();
                                recibo.setRecibo(f.getName());
                                ArquivosFTP.add(recibo);
                            }
                        }
                        // Adapter
                        ReciboAdapter adapter = new ReciboAdapter(ArquivosFTP);
                        recyclerRecibo.setAdapter(adapter);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

My Receipt Card:

public class ReciboAdapter extends RecyclerView.Adapter<ReciboAdapter.ReciboHolder> {

    List<Recibo> listaRecibo;

    public ReciboAdapter(List<Recibo> listaRecibo) {
        this.listaRecibo = listaRecibo;
    }

    @NonNull
    @Override
    public ReciboHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View vista = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lista_recibo, viewGroup, false);
        RecyclerView.LayoutParams layoutParams =
                new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        vista.setLayoutParams(layoutParams);
        return new ReciboHolder(vista);
    }

    @Override
    public void onBindViewHolder(@NonNull ReciboHolder reciboHolder, int i) {

        reciboHolder.txtRecibo.setText(listaRecibo.get(i).getRecibo().toString());
    }

    @Override
    public int getItemCount() {
        return listaRecibo.size();
    }

    public class ReciboHolder extends RecyclerView.ViewHolder {

        TextView txtRecibo;

        public ReciboHolder(@NonNull View itemView) {
            super(itemView);

            txtRecibo = (TextView) itemView.findViewById(R.id.txtRecibo);
        }
    }

}
  • Maybe you need the runOnUIThread()

  • I will search and try to implement @Rocigno Medeiros.

1 answer

-2


Try it this way:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        recyclerRecibo.setAdapter(adapter);  
    }
});
  • It didn’t work, it was the same thing, the recyclerView without registration and the logcat showed this line: E/Recyclerview: No adapter attached; skipping layout

  • I got it, I was putting it in a wrong part of the code. vlw...

Browser other questions tagged

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