How to list directory in a listview?

0

I need to list a specific sdcard directory, in a custom listview. See how it works, the list contains a button 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 do not know if it's the correct way, but how to apply it in my listview? in my list is manually loading some items from the string.xml, I want to list the files in the directory.

    
asked by anonymous 26.01.2015 / 19:00

1 answer

0

This is the minimum to get a list of 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));

Want something else complete ? Read this:

link

    
26.01.2015 / 19:14