ListActivity显示根目录的文件与目录

java:

package EX04_21.txt;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.R.string;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EX04_21 extends ListActivity {
	private String rootPath = "/";
	private TextView mTextView;
	private List<String> items = null;
	private List<String> paths = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTextView = (TextView)findViewById(R.id.myTextView);
        getFilesDir(rootPath);
    }
    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	String currentPath = mTextView.getText().toString();
		File file = new File(currentPath);
		if (!currentPath.equals(rootPath)) {
			getFilesDir(file.getParent());
			return;
		}
    	super.onBackPressed();
    }
    
    private void getFilesDir(String Path) {    	
		// TODO Auto-generated method stub    	    	
		File file = new File(Path);				
		if (!file.canRead()) {
			Toast.makeText(EX04_21.this, file.getName()+"为不可读目录。", Toast.LENGTH_SHORT).show();
			return;
		}
		else {
			mTextView.setText(Path);
	    	items = new ArrayList<String>();
	    	paths = new ArrayList<String>();
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				File f = files[i];
				items.add(f.getName());
				paths.add(f.getPath());
			}
		}
		
		ArrayAdapter<String> array = new ArrayAdapter<String>(EX04_21.this, 
				R.layout.my_list_style, items);
		
		setListAdapter(array);
	}
    private void getFilesDirEx(String Path) {    	
		// TODO Auto-generated method stub    	
    	mTextView.setText(Path);
    	items = new ArrayList<String>();
    	paths = new ArrayList<String>();    	
		File file = new File(Path);
		File[] files = file.listFiles();
		if (files == null) {
			Toast.makeText(EX04_21.this, "鸟蛋", Toast.LENGTH_LONG).show();
		} 

	}
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	// TODO Auto-generated method stub
		
		File file = new File(paths.get(position));
		if (file.isDirectory()) {			
			getFilesDir(paths.get(position));			
		} else {
			new AlertDialog.Builder(EX04_21.this)
				.setIcon(R.drawable.icon)
				.setTitle(file.getName()+"属性")
				.setMessage(file.getName()+"不是文件夹,是文件。")
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				}).show();
		}
    	super.onListItemClick(l, v, position, id);
    }
}

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/myTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding = "5px"
    android:textSize = "18sp"
    />
 <ListView 
	android:id = "@android:id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />   
</LinearLayout>

my_list_style.xml

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值