第二章:java I/O应用(二)

效果:


[img]http://dl.iteye.com/upload/attachment/389324/6a79e37d-6538-3de0-a9e6-9c34abbb33d9.jpg[/img]


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mPath"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>




在res/layout/下新建一个file_row.xml

<?xml version="1.0" encoding="UTF-8"?>
<TextView
android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="20px"
android:textSize="14sp"/>





package i.o.and.listActiviy;

import java.io.File;

import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class IOAndListActivity extends ListActivity {
private List<String> items=null;
private List<String> paths=null;
private String rootPath="/";
private TextView mPath;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**载入 main.xml*/
setContentView(R.layout.main);
/**通过id找到TextView组件*/
mPath=(TextView)findViewById(R.id.mPath);
getFileDir(rootPath);
}
/**获得文件路径构架*/
public void getFileDir(String filePath){
/**当前路径*/
mPath.setText(filePath);
items=new ArrayList<String>();
paths=new ArrayList<String>();
File f=new File(filePath);
File[] files=f.listFiles();

if(!filePath.equals(rootPath)){
/**第一条设置为会到目录*/
items.add("返回"+rootPath);
paths.add(rootPath);
/**返回上一层*/
items.add("返回../");
paths.add(f.getParent());
}
/**将文件名字和路径添加到List当中*/
if(files!=null){
for(int i=0;i<files.length;i++){
File file=files[i];
items.add(file.getName());
paths.add(file.getPath());
}
}
/**使用layout.file_row,items将Adapter设置给ListActivity*/
ArrayAdapter<String> fileList=new ArrayAdapter<String>(this,R.layout.file_row,items);
setListAdapter(fileList);
}
/**设置items被点击的事件*/
protected void onListItemClick(ListView l,View v,int position,long id){
File file=new File(paths.get(position));
if(file.isDirectory()){
/**如果是文件夹就进去*/
getFileDir(paths.get(position));
}else{
/**如果是文件就告诉用户是文件*/
new AlertDialog.Builder(this).setIcon(R.drawable.icon).setTitle("["+file.getName()+"] 是文件!").setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
}).show();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值