想把SD卡以树形结构显示要如何做啊 是把小弟我下列的程序和Tree结合吗? 如果是结合要如何做
想把SD卡以树形结构显示要怎么做啊? 是把我下列的程序和Tree结合吗? 如果是结合要怎么做啊
public class MainActivity extends Activity {
ListView listView;
TextView textView;
//记录当前的父文件夹
File currentParent;
//记录当前路径下的所有文件的文件数组
File[] currentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//列出全部文件的listview
listView = (ListView) this.findViewById(R.id.list);
textView = (TextView) this.findViewById(R.id.path);
//获取系统SD卡的目录
File root = new File("/mnt/sdcard/");
//如果SD卡存在
if (root.exists()) {
currentParent = root;
currentFiles = root.listFiles();
//使用当前目录下的全部文件、文件夹来填充listview
inflateListView(currentFiles);
}
//为listview的列表项的单击事件绑定监听器
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//用户单击了文件夹,直接返回,不做任何处理
if (currentFiles[position].isFile()) return;
//获取用户点击的文件夹下的说有文件
File[] tmp = currentFiles[position].listFiles();
if (tmp ==null || tmp.length == 0) {
Toast.makeText(MainActivity.this, "当前路径不可访问或该路径下没有文件", Toast.LENGTH_SHORT).show();
} else {
//获取用户单击的列表项对应的文件夹,设为当前的父文件夹
currentParent = currentFiles[position];
//保存当前的父文件夹内的全部文件和文件夹
currentFiles = tmp;
inflateListView(currentFiles);
}
}
});
//获取上一级目录的按钮
Button parent = (Button) this.findViewById(R.id.parent);
parent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (!currentParent.getCanonicalPath().equals("/mnt/sdcard")) {
//获取上一级目录
currentParent = currentParent.getParentFile();
//列出当前目录下的所有文件
currentFiles = currentParent.listFiles();
//再次更新listview
inflateListView(currentFiles);
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
private void inflateListView( File[] files) {
// TODO Auto-generated method stub
ArrayList<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0; i < files.length; i ++) {
HashMap<String, Object> listItem = new HashMap<String, Object>();
//如果当前File是文件夹,使用cx图标,否则使用w图标
if (files[i].isDirectory()) {
listItem.put("icon", R.drawable.cx);
} else {
listItem.put("icon", R.drawable.w);
}
listItem.put("fileName", files[i].getName());
//添加arraylist项
listItems.add(listItem);
}
//创建一个SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.line, new String[] {"icon", "fileName"},
new int[] {R.id.icon, R.id.file_name});
//为;listview设置adapter
public class MainActivity extends Activity {
ListView listView;
TextView textView;
//记录当前的父文件夹
File currentParent;
//记录当前路径下的所有文件的文件数组
File[] currentFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//列出全部文件的listview
listView = (ListView) this.findViewById(R.id.list);
textView = (TextView) this.findViewById(R.id.path);
//获取系统SD卡的目录
File root = new File("/mnt/sdcard/");
//如果SD卡存在
if (root.exists()) {
currentParent = root;
currentFiles = root.listFiles();
//使用当前目录下的全部文件、文件夹来填充listview
inflateListView(currentFiles);
}
//为listview的列表项的单击事件绑定监听器
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//用户单击了文件夹,直接返回,不做任何处理
if (currentFiles[position].isFile()) return;
//获取用户点击的文件夹下的说有文件
File[] tmp = currentFiles[position].listFiles();
if (tmp ==null || tmp.length == 0) {
Toast.makeText(MainActivity.this, "当前路径不可访问或该路径下没有文件", Toast.LENGTH_SHORT).show();
} else {
//获取用户单击的列表项对应的文件夹,设为当前的父文件夹
currentParent = currentFiles[position];
//保存当前的父文件夹内的全部文件和文件夹
currentFiles = tmp;
inflateListView(currentFiles);
}
}
});
//获取上一级目录的按钮
Button parent = (Button) this.findViewById(R.id.parent);
parent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (!currentParent.getCanonicalPath().equals("/mnt/sdcard")) {
//获取上一级目录
currentParent = currentParent.getParentFile();
//列出当前目录下的所有文件
currentFiles = currentParent.listFiles();
//再次更新listview
inflateListView(currentFiles);
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
private void inflateListView( File[] files) {
// TODO Auto-generated method stub
ArrayList<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0; i < files.length; i ++) {
HashMap<String, Object> listItem = new HashMap<String, Object>();
//如果当前File是文件夹,使用cx图标,否则使用w图标
if (files[i].isDirectory()) {
listItem.put("icon", R.drawable.cx);
} else {
listItem.put("icon", R.drawable.w);
}
listItem.put("fileName", files[i].getName());
//添加arraylist项
listItems.add(listItem);
}
//创建一个SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.line, new String[] {"icon", "fileName"},
new int[] {R.id.icon, R.id.file_name});
//为;listview设置adapter