新手刚学习安卓,写了个程序
根目录是scard,点击文件夹可以浏览其子目录所有文件,
比如点击一个文件夹,怎样实现返回父目录,求教高手主程序代码:
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;public class ExplorerActivity extends ListActivity{
private String rootPath = "/sdcard";
private String currentPath = "";
private ArrayList<String> paths=new ArrayList<String>(); ListView mListView = null;
ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();
SimpleAdapter adapter = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mListView = (ListView)findViewById(R.id.fileList);
currentPath = rootPath;
showList();
}
private void showList(){
mData = getDir(currentPath);
adapter = new SimpleAdapter(this,mData,R.layout.mylist,
new String[]{"image","title"},new int[]{R.id.list_image,R.id.list_title});
mListView.setAdapter(adapter);
mListView.setSelection(0);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
currentPath = paths.get(position);
File f = new File(currentPath);
if(f.isDirectory()){
fresh(currentPath);
} else {
Toast.makeText(ExplorerActivity.this, "this is not a directory", Toast.LENGTH_SHORT).show();
}
}
});
}
private void fresh(String path){
mData.clear();
mData = getDir(path);
adapter.notifyDataSetChanged();
} private ArrayList<Map<String, Object>> getDir(String path){
mListView = getListView();
paths = new ArrayList<String>();
File f = new File(path);
File fileList[] = f.listFiles();
int length = fileList.length; for(int i =0; i < length; i++) {
Map<String,Object> item = new HashMap<String,Object>();
item.put("image", R.drawable.file_icon);
item.put("title", fileList[i].getName());
File file=fileList[i];
paths.add(file.getPath());
mData.add(item);
}
return mData;
}
}main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ListView
android:id="@+id/fileList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>mylist.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fastScrollEnabled="true"
android:layout_width="fill_parent"
android:layout_height="40dp"> <ImageView
android:id="@+id/list_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/list_image"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textSize="20sp"
android:singleLine="true"
/>
</RelativeLayout>
根目录是scard,点击文件夹可以浏览其子目录所有文件,
比如点击一个文件夹,怎样实现返回父目录,求教高手主程序代码:
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;public class ExplorerActivity extends ListActivity{
private String rootPath = "/sdcard";
private String currentPath = "";
private ArrayList<String> paths=new ArrayList<String>(); ListView mListView = null;
ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();
SimpleAdapter adapter = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mListView = (ListView)findViewById(R.id.fileList);
currentPath = rootPath;
showList();
}
private void showList(){
mData = getDir(currentPath);
adapter = new SimpleAdapter(this,mData,R.layout.mylist,
new String[]{"image","title"},new int[]{R.id.list_image,R.id.list_title});
mListView.setAdapter(adapter);
mListView.setSelection(0);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
currentPath = paths.get(position);
File f = new File(currentPath);
if(f.isDirectory()){
fresh(currentPath);
} else {
Toast.makeText(ExplorerActivity.this, "this is not a directory", Toast.LENGTH_SHORT).show();
}
}
});
}
private void fresh(String path){
mData.clear();
mData = getDir(path);
adapter.notifyDataSetChanged();
} private ArrayList<Map<String, Object>> getDir(String path){
mListView = getListView();
paths = new ArrayList<String>();
File f = new File(path);
File fileList[] = f.listFiles();
int length = fileList.length; for(int i =0; i < length; i++) {
Map<String,Object> item = new HashMap<String,Object>();
item.put("image", R.drawable.file_icon);
item.put("title", fileList[i].getName());
File file=fileList[i];
paths.add(file.getPath());
mData.add(item);
}
return mData;
}
}main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ListView
android:id="@+id/fileList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>mylist.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fastScrollEnabled="true"
android:layout_width="fill_parent"
android:layout_height="40dp"> <ImageView
android:id="@+id/list_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/list_image"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textSize="20sp"
android:singleLine="true"
/>
</RelativeLayout>
解决方案 »
- 关于android显示子系统的认识-请指教
- 安卓手机adb devices重号安装apk问题
- 请教android2.2怎么格式化SDcard?
- android个人开发者的图片素材从何而来
- android 4.0 使用intent 调用照相机与直接打开系统照相机效果不同
- ADT LogCat 里只显示W I E的信息
- 有个Calendar的问题不明白
- listview的getview中如何不添加该条list?
- 继续上一个项目SQLiteDatabase的User类登陆注册Student类增删改查
- Android
- 请叫个问题,用NDK做的动态库的问题。
- activity下的值,MysurfaceView怎么取?
{
mListView = getListView();
paths = new ArrayList<String>();
File f = new File(path);
File fileList[] = f.listFiles();
int length = fileList.length;
if(length<1)
{
if(f.getParent()!=null)
{
Map<String,Object> item1 = new HashMap<String,Object>();
item1.put("image", R.drawable.file_icon);
item1.put("title", "..");
mData.add(item1);
paths.add(f.getParent());
}
}
else
{
for(int i =0; i < length; i++)
{
File file=fileList[i];
if(i==0)
{
if(f.getParent()!=null)
{
Map<String,Object> item1 = new HashMap<String,Object>();
item1.put("image", R.drawable.file_icon);
item1.put("title", "..");
mData.add(item1);
paths.add(f.getParent());
}
}
paths.add(file.getPath());
Map<String,Object> item = new HashMap<String,Object>();
if(file.isDirectory())
{
item.put("image", R.drawable.file_icon);
}
else
{
item.put("image", R.drawable.file_icon);
}
item.put("title", fileList[i].getName());
mData.add(item);
}
}
return mData;
}