文件查看器,怎样实现返回查看功能,该怎么解决
文件查看器,怎样实现返回查看功能
新手刚学习安卓,写了个程序
根目录是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"?>
新手刚学习安卓,写了个程序
根目录是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"?>