Recyclerview does not show list data

Asked

Viewed 171 times

-1

I have a problem that when trying to list the data on RecyclerView it does not show the records, in Debug I check that the records are entered in the list 'ArrayList', follows the code.

Method ListarArquivosdoFTP where it connects with FTP and returns the string of the items and plays the list to the Adapter:

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);
                            }
                        }
                        ReciboAdapter adapter = new ReciboAdapter(ArquivosFTP);
                        recyclerRecibo.setAdapter(adapter);
                    }

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

And mine Adapter complete:

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);
        }
    }
}

And in the onCreateView is like this:

recyclerRecibo = (RecyclerView) view.findViewById(R.id.idRecycler);
recyclerRecibo.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerRecibo.setHasFixedSize(true);
  • I think it’s because Layoutmanager missed, add that line recyclerRecibo.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false)); before setting the Adapter

  • I already have on onCreateView, I will add on the question.

1 answer

0

This should work for you:

    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);
                        }
                    }
                    ReciboAdapter adapter = new ReciboAdapter(ArquivosFTP);
                    recyclerRecibo = (RecyclerView) view.findViewById(R.id.idRecycler);
                    recyclerRecibo.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerRecibo.setHasFixedSize(true);
                    recyclerRecibo.setAdapter(adapter);
                }

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

Finally, in onCreateView(), call the Listingquivosftp method();

Note: The recyclerRecibo.setAdapter(adapter); has to be the last of all Recyclerview settings.

Browser other questions tagged

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