Android 快速实现一个文件选择器
2010-02-24 04:48:00 来源:WEB开发网lv2.setAdapter(ap);
ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
// "_id", "ext_number", "name","ann", "intro"
int fid=ap.getItemType((int)id);
String mPath="";
if (fid==1){
String s1=ap.getItem((int)id).name;
if (s1.equals("..")){
mPath=mCurrentDirectory.getParent();
}else{
mPath=mCurrentDirectory.getPath()+"/"+s1+"/";
}
mCurrentDirectory=new File(mPath);
ListFile(mCurrentDirectory);
}else{
Bundle bundle = new Bundle();
bundle.putString("filename",mCurrentDirectory.getPath()+"/"+ ap.getItem((int)id).name);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
frm_openfile.this.finish();
}
}
};
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();
fd.name=f.getName();
fd.type=1;
ap.addItem(fd);
}else{
if (checkEnds(f.getName().toLowerCase())){
fileData fd=new fileData();
fd.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;
}
}
很简单,是不?
更多精彩
赞助商链接