===EDIT===
Is there a reason something like this doesn’t work i feel like it should but it won’t work lView.getChildCount() returns 0 even though there is a lot in the list and then this causes a force close
for (int i = 0; i < lView.getChildCount(); i++) {
ImageView imageView = (ImageView) lView.getChildAt(i).findViewById(R.id.listIcon);
imageView.setImageResource(R.drawable.someIcon);
}
==================================
First i know you can make custom adapters and all i’m just thinking for my purposes since this is a simple file browser nothing advanced maybe this can be done.
Anyway like i said I’m working on a very simple file browser for part of an app i’m writing. I set it up to show all the files in a directory and have an icon next to it. The icon at the moment is the same for each file but i would like to have an icon for each type of file (ie zips, folders, scripts, etc)
What i would like to do is after the array and all has been made just do like for each position check the item and if it ends in say .zip change the icon something along the lines of this
for(eachposition){
if(position.endsWith(".zip"){
ImageView iv = (ImageView) findViewById(R.id.listIcon);
iv.setImageResource(R.drawable.zipImage);
}else if(position.endsWith(".sh"){
ImageView iv = (ImageView) findViewById(R.id.listIcon);
iv.setImageResource(R.drawable.scriptImage);
}else{
ImageView iv = (ImageView) findViewById(R.id.listIcon);
iv.setImageResource(R.drawable.generalImage);
}
}
anyway here is the code i’m using currently just in case this is needed and thank you for any help or input
public class SampleList extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root = "/sdcard/somefolder/";
private String current = null;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
context = getApplicationContext();
try{
getDir(root);
File directory = new File(root);
File[] contents = directory.listFiles();
if (contents.length == 0) {
TextView t = new TextView(this);
t=(TextView)findViewById(R.id.empty);
t.setText(R.string.emptyFol);
}
}catch (Exception e) {
Toast.makeText(NandPicker.this, R.string.nMount, Toast.LENGTH_LONG).show();
}
}
private void getDir(String dirPath){
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
for(int i=0; i < files.length; i++){
File file = files[i];
//Problem was adding the path without the /
//path.add(file.getPath());
if(file.isDirectory()){
path.add(file.getPath()+"/");
item.add(file.getName() + "/");
}else{
path.add(file.getPath());
item.add(file.getName());
}
}
current = dirPath;
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.rowTextView, item);
setListAdapter(fileList);
class IgnoreCaseComparator implements Comparator<String> {
public int compare(String strA, String strB) {
return strA.compareToIgnoreCase(strB);
}
}
IgnoreCaseComparator icc = new IgnoreCaseComparator();
java.util.Collections.sort(path,icc);
java.util.Collections.sort(item,icc);
}
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
}
}
Take a look at an example here. This is covered quite often.