把一个虚拟sd卡内的文件和文件夹以树形结构显示在虚拟机上要怎么做啊 详细一点啊 本人初学者 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是个很有深度的问题。如果你考虑文件夹可以折叠展开的话,就没有一个系统给你提供的listViewandroid本身没有提供TreeListView只提供了一个ExpandableListViewExpandableListView只有两层所以你可以google一下android TreeListView如果你只是想把所有的文件夹和文件列出来,那么用迭代遍历的方法,把所有文件存到list中显示到listView中就可以了。 有没有代码参考啊 我是新手刚刚接触android 你是想要treelistview的吗?目前还没有,你可以去网上找找。我也在研究。打算写个TreeListAdapter和TreeExpandableListView,不过老是拖着,现在还没开始写。不过你可以先看看expandableListview学会了这个双层结构,在学习多层结构。也可以去google搜搜treeview,应该会有demo。新手,所以才慢慢来。先去看看expandableListview是不是你要的效果。 两层不够用啊....大神可不可以用listview来做呢要是可以的话要怎么来做啊? 两层的确不够用。listview可以实现你让我想想,写个简单的demo。你可以去网上搜搜。多层文件夹的实现。 这是一个demo,里面存在一些问题和bug,性能也不好,不过大概用listview实现了你的要求,如果需要完善,最好还是研究研究如何实现treeListView最好。MainActivity package com.example.zdemo2;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;public class MainActivity extends Activity { //存放路径数据 private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); //listview private ListView lv; //adapter private MyAdapter ma; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView1); //初始化数据,将sd卡第一层的文件读入data initData(); ma = new MyAdapter(MainActivity.this, data); lv.setAdapter(ma); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //拿到要展开的文件list List<HashMap<String, String>> tempList = expand(position); //如果该文件夹下有文件 if(tempList.size()>0){ //获得该文件夹的状态 true = 打开 false = 关闭 boolean isOpen = "1".equals(data.get(position).get("isOpen")); //关闭状态下 if(!isOpen){ //list中添加子文件夹列表 data.addAll(position + 1, tempList); //设置该文件为打开状态 data.get(position).put("isOpen", "1"); //为adapter更新list ma.setData(data); //刷新adapter ma.notifyDataSetChanged(); } //打开状态下 else{ //关闭子文件夹,并从data中移除。 shrink(position); //设置该文件为关闭状态 data.get(position).put("isOpen", "0"); //为adapter更新list ma.setData(data); //刷新adapter ma.notifyDataSetChanged(); } } } }); } private void initData() { //sd卡路径 String directoryPath = Environment.getExternalStorageDirectory() .toString(); //sd卡路径生成的文件夹 File dir = new File(directoryPath); //如果sd卡是文件夹 if (dir.isDirectory()) { //拿到sd卡下文件列表 File[] files = dir.listFiles(); HashMap<String, String> tempHash = new HashMap<String, String>(); for (File f : files) { tempHash = new HashMap<String, String>(); //将相关信息存入hashmap //文件名 tempHash.put("name", f.getName()); //父值 tempHash.put("root", "0"); //本节点值 tempHash.put("node", "1"); //路径 tempHash.put("path", f.getPath()); //是否被打开,0 = 否 tempHash.put("isOpen", "0"); //把该文件添加到list中 data.add(tempHash); } } } //打开子目录 private List<HashMap<String, String>> expand(int position) { List<HashMap<String, String>> result = new ArrayList<HashMap<String,String>>(); //拿到路径生成file File dir = new File(data.get(position).get("path")); //如果是文件夹 if (dir.isDirectory()) { //下面参照初始化 File[] files = dir.listFiles(); HashMap<String, String> tempHash = new HashMap<String, String>(); for (File f : files) { tempHash = new HashMap<String, String>(); tempHash.put("name", f.getName()); tempHash.put("root", data.get(position).get("node")); int tempInt = Integer.parseInt(data.get(position).get("node")); String node = String.valueOf(tempInt + 1); tempHash.put("node", node); tempHash.put("path", f.getPath()); tempHash.put("isOpen", "0"); result.add(tempHash); } } return result; } //关闭 private void shrink(int position){ boolean flag = true; while(flag){ //拿到当前节点值 int p1 = Integer.parseInt(data.get(position).get("node")); //列表下一个节点值 int p2 = Integer.parseInt(data.get(position+1).get("node")); //当前节点值小于下一节点值 (当前节点是下节点的父或祖) if(p1<p2){ //移除 data.remove(position+1); } else{ //结束循环 flag = false; } } }}MyAdapter package com.example.zdemo2;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MyAdapter extends BaseAdapter { private Context context; //用来进行缩进的string private static final String SPACE = " "; private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); public MyAdapter(Context c, List<HashMap<String, String>> data) { super(); this.context = c; this.data = data; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //新建一个textview来返回 TextView tv = new TextView(context); //缩进值,不会写这个单词的英文用拼音代替。 int suojin = Integer.parseInt(data.get(position).get("node")) - 1; //textview显示的内容 String message = ""; //对textview进行缩进 for (int i = 0; i < suojin; i++) { message += SPACE; } //把文件名放入textview message += data.get(position).get("name"); tv.setText(message); return tv; } public void setData(List<HashMap<String, String>> data) { this.data = data; }}activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView></RelativeLayout> 使用PopupWindow出现的错误 android ==号没用????不会把 怎么回事 作为Android手机的服务器,如何选择? activity 里面的this 怎么理解? 打开系统游览器. android传递字符数组的时候挂掉了 新手!Android为button添加事件问题 Eclipse里如何导入别人的android项目 android 方法被划了线,提示不可用怎么办 请问有没有在android app添加广告条的教程? Android widget如何添加到桌面 andriod开发中eclipse中的ctrl+c不能用
如果你考虑文件夹可以折叠展开的话,就没有一个系统给你提供的listView
android本身没有提供TreeListView
只提供了一个ExpandableListView
ExpandableListView只有两层所以你可以google一下android TreeListView如果你只是想把所有的文件夹和文件列出来,那么用迭代遍历的方法,把所有文件存到list中显示到listView中就可以了。
目前还没有,你可以去网上找找。我也在研究。
打算写个TreeListAdapter和TreeExpandableListView,不过老是拖着,现在还没开始写。
不过你可以先看看expandableListview
学会了这个双层结构,在学习多层结构。
也可以去google搜搜treeview,应该会有demo。
新手,所以才慢慢来。先去看看expandableListview是不是你要的效果。
大神可不可以用listview来做呢
要是可以的话要怎么来做啊?
listview可以实现
你让我想想,写个简单的demo。你可以去网上搜搜。多层文件夹的实现。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;public class MainActivity extends Activity { //存放路径数据
private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); //listview
private ListView lv; //adapter
private MyAdapter ma;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
//初始化数据,将sd卡第一层的文件读入data
initData();
ma = new MyAdapter(MainActivity.this, data);
lv.setAdapter(ma);
lv.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//拿到要展开的文件list
List<HashMap<String, String>> tempList = expand(position);
//如果该文件夹下有文件
if(tempList.size()>0){
//获得该文件夹的状态 true = 打开 false = 关闭
boolean isOpen = "1".equals(data.get(position).get("isOpen"));
//关闭状态下
if(!isOpen){
//list中添加子文件夹列表
data.addAll(position + 1, tempList);
//设置该文件为打开状态
data.get(position).put("isOpen", "1");
//为adapter更新list
ma.setData(data);
//刷新adapter
ma.notifyDataSetChanged();
}
//打开状态下
else{
//关闭子文件夹,并从data中移除。
shrink(position);
//设置该文件为关闭状态
data.get(position).put("isOpen", "0");
//为adapter更新list
ma.setData(data);
//刷新adapter
ma.notifyDataSetChanged();
}
}
}
});
} private void initData() {
//sd卡路径
String directoryPath = Environment.getExternalStorageDirectory()
.toString();
//sd卡路径生成的文件夹
File dir = new File(directoryPath);
//如果sd卡是文件夹
if (dir.isDirectory()) {
//拿到sd卡下文件列表
File[] files = dir.listFiles();
HashMap<String, String> tempHash = new HashMap<String, String>();
for (File f : files) {
tempHash = new HashMap<String, String>();
//将相关信息存入hashmap
//文件名
tempHash.put("name", f.getName());
//父值
tempHash.put("root", "0");
//本节点值
tempHash.put("node", "1");
//路径
tempHash.put("path", f.getPath());
//是否被打开,0 = 否
tempHash.put("isOpen", "0");
//把该文件添加到list中
data.add(tempHash);
}
}
} //打开子目录
private List<HashMap<String, String>> expand(int position) {
List<HashMap<String, String>> result = new ArrayList<HashMap<String,String>>();
//拿到路径生成file
File dir = new File(data.get(position).get("path"));
//如果是文件夹
if (dir.isDirectory()) {
//下面参照初始化
File[] files = dir.listFiles();
HashMap<String, String> tempHash = new HashMap<String, String>();
for (File f : files) {
tempHash = new HashMap<String, String>();
tempHash.put("name", f.getName());
tempHash.put("root", data.get(position).get("node"));
int tempInt = Integer.parseInt(data.get(position).get("node"));
String node = String.valueOf(tempInt + 1);
tempHash.put("node", node);
tempHash.put("path", f.getPath());
tempHash.put("isOpen", "0");
result.add(tempHash);
}
}
return result;
}
//关闭
private void shrink(int position){
boolean flag = true;
while(flag){
//拿到当前节点值
int p1 = Integer.parseInt(data.get(position).get("node"));
//列表下一个节点值
int p2 = Integer.parseInt(data.get(position+1).get("node"));
//当前节点值小于下一节点值 (当前节点是下节点的父或祖)
if(p1<p2){
//移除
data.remove(position+1);
}
else{
//结束循环
flag = false;
}
}
}
}MyAdapter package com.example.zdemo2;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;public class MyAdapter extends BaseAdapter { private Context context; //用来进行缩进的string
private static final String SPACE = " "; private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); public MyAdapter(Context c, List<HashMap<String, String>> data) {
super();
this.context = c;
this.data = data;
} @Override
public int getCount() {
return data.size();
} @Override
public Object getItem(int position) {
return data.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
//新建一个textview来返回
TextView tv = new TextView(context);
//缩进值,不会写这个单词的英文用拼音代替。
int suojin = Integer.parseInt(data.get(position).get("node")) - 1;
//textview显示的内容
String message = "";
//对textview进行缩进
for (int i = 0; i < suojin; i++) {
message += SPACE;
}
//把文件名放入textview
message += data.get(position).get("name");
tv.setText(message);
return tv;
} public void setData(List<HashMap<String, String>> data) {
this.data = data;
}}activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView></RelativeLayout>