Android 快速实现一个文件选择器
2010-02-24 04:48:00 来源:WEB开发网}
}
};
ListFile(mCurrentDirectory);
lv2.setOnItemClickListener(lv2click);
}
private void ListFile(File aDirectory) {
ap.clearItems();
ap.notifyDataSetChanged();
lv2.postInvalidate();
Log.v("vodone","mpath="+aDirectory.getPath());
//if (aDirectory==null)return;
if (!aDirectory.getPath().equals("/sdcard")){
fileData fd=new fileData();
fd.name="..";
fd.type=1;
ap.addItem(fd);
}
for( File f : aDirectory.listFiles() ){
if (f.isDirectory()){
fileData fd=new fileData();
ffd.name=f.getName();
fd.type=1;
ap.addItem(fd);
}else{
if (checkEnds(f.getName().toLowerCase())){
fileData fd=new fileData();
ffd.name=f.getName();
fd.type=0;
ap.addItem(fd);
}
}
}
ap.notifyDataSetChanged();
lv2.postInvalidate();
}
private boolean checkEnds(String checkItsEnd) {
for (String aEnd : fileEndings) {
if (checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
}
public class frm_openfile extends Activity implements OnClickListener {
ListView lv2;
private File mCurrentDirectory = new File("/sdcard/");
ExpBaseAdapter ap;
String fileEndings[]={"png","bmp","jpg","jpeg","avi","3gp","mp3","mp4"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.text_selectfile);
setContentView(R.layout.frm_openfile);
lv2 = (ListView)findViewById(R.id.of_lv);
lv2.setCacheColorHint(0x00000000);
ap=new ExpBaseAdapter(this);
更多精彩
赞助商链接