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.