How to list directory in a listview?

Asked

Viewed 2,435 times

0

I need to list a specific sdcard directory in a custom listview. Look at how I work, the list contains a boot and a title:

Adapter

public class AdapterListView extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<Lista> itens;

    public AdapterListView(Context context, ArrayList<Lista> itens) {
        //Itens que preencheram o listview
        this.itens = itens;
        //responsavel por pegar o Layout do item.
        mInflater = LayoutInflater.from(context);
    }

    /**
     * Retorna a quantidade de itens
     *
     * @return
     */
    public int getCount() {
        return itens.size();
    }

    /**
     * Retorna o item de acordo com a posicao dele na tela.
     *
     * @param position
     * @return
     */
    public Lista getItem(int position) {
        return itens.get(position);
    }

    /**
     * Sem implementação
     *
     * @param position
     * @return
     */
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View view, ViewGroup parent) {
        //Pega o item de acordo com a posção.
        Lista item = itens.get(position);
        //infla o layout para podermos preencher os dados
        view = mInflater.inflate(R.layout.item_list, null);

        //atravez do layout pego pelo LayoutInflater, pegamos cada id relacionado
        //ao item e definimos as informações.
        ((TextView) view.findViewById(R.id.text)).setText("Texto");
        ((Button) view.findViewById(R.id.imageview)).setText("meu botão");
      //  ((TextView) view.findViewById(R.id.subtitulo)).setText(item.getSubtitulo());

        return view;
    }
}

Activity

public void criarLista() {

    itens = new ArrayList<Lista>();

    String[] categorias = getResources().getStringArray(R.array.titulos);

    String[] drawableCategorias = getResources().getStringArray(
            R.array.botao_imagem);

    for (int i = 0; i < categorias.length; ++i) {
        itens.add(new Lista(categorias[i], getResources().getIdentifier(
                drawableCategorias[i], "drawable", this.getPackageName())));
    }


    adapter = new AdapterListView(DownloadView.this, itens);        // Cria o adapter adapter = new AdapterListView(this, itens);

    listView.setAdapter(adapter); // Cor quando a lista é
    listView.setCacheColorHint(Color.TRANSPARENT);

}

I found a method to list:

File dir = new File(dirPath);
File[] filelist = dir.listFiles();
String[] theNamesOfFiles = new String[filelist.length];
for (int i = 0; i < theNamesOfFiles.length; i++) {
   theNamesOfFiles[i] = filelist[i].getName();
}

I don’t know if it’s the right way, but how do I apply it to my listview? in my listturn this manually loading some la items from the.xml string, I want to list the directory files.

1 answer

0


This is the minimum to get a list from a directory:

    private File file;
    private List<String> minhaLista;

    minhaLista = new ArrayList<String>();   

    String root_sd = Environment.getExternalStorageDirectory().toString();
    file = new File( root_sd + "/pasta" ) ;       
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            minhaLista.add( list[i].getName() );
    }

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, minhaLista));

Wants something more complete? Read this:

https://www.codeofaninja.com/2013/02/android-listview-tutorial.html

  • But I have List<List> a List class , which I created with two objects, and a constructor.when I change to List<List> mylist; from the error in the add method, put addAll, as I do in my context?

  • You are SURE that you have a code that creates this path: file = new File( root_sd + "/pasta" ); IF THERE IS NO SUCH PATH, YOU WILL LIST NOTHING!

  • I changed my Adapter everything to string, to adapt in your code, yes I already have a directory with the name Freedom, and some files there in the folder, but not list anything

  • If the Freedom folder is in the SDCARD, it should look like this: file = new File( root_sd + "/freedom" );...But what’s your goal in listing ÂRQUIVOS from a directory?

  • It worked!! in my application I have a small file manager. Thanks

  • FANTASTIC!!!!

Show 2 more comments

Browser other questions tagged

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