首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

NotePad学习札记二

2012-09-22 
NotePad学习笔记二根据学习笔记一完成一些代码,贴出来看看:Notepad.javapackage com.example.notepadimpo

NotePad学习笔记二

根据学习笔记一完成一些代码,贴出来看看:

Notepad.java

package com.example.notepad;import java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;// TODO: Auto-generated Javadoc/** * The Class OpenNote. */public class OpenNote extends Activity {    /** Called when the activity is first created. */    private ListView listview;        /** The items. */    private ArrayList<String> items = null;        /** The paths. */    private ArrayList<String> paths = null;        /** The root path. */    private String rootPath = "/"; // 根目录        /** The m path. */    private TextView mPath; // 当前路径        /** The currentpath. */    String currentpath;        /** The list items. */    private List<Map<String, Object>> listItems = null;    /* (non-Javadoc)     * @see android.app.Activity#onCreate(android.os.Bundle)     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.open_note);        init();        getFileDir(rootPath);    }    /**     * Gets the file dir.     *     * @param filePath the file path     * @return the file dir     */    private void getFileDir(String filePath) {        mPath.setText("当前目录:" + filePath);        currentpath = filePath;        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();        items = new ArrayList<String>();        paths = new ArrayList<String>();        File f = new File(filePath);        File[] files = f.listFiles();        Map<String, Object> listItem = new HashMap<String, Object>();        if (!filePath.equals(rootPath)) {            items.add("cd /");            paths.add(rootPath);            items.add("cd ..");            paths.add(f.getParent());            listItem.put("header", R.drawable.back01);            listItem.put("name", "cd /");            listItems.add(listItem);            Map<String, Object> listItem1 = new HashMap<String, Object>();            listItem1.put("header", R.drawable.back02);            listItem1.put("name", "cd ..");            listItems.add(listItem1);        }        for (int i = 0; i < files.length; i++) {            Map<String, Object> listItem2 = new HashMap<String, Object>();            File file = files[i];            items.add(file.getName());            paths.add(file.getPath());            if (file.isDirectory()) {                listItem2.put("header", R.drawable.folder);            }            else {                String pathStr = file.getAbsolutePath();                if (pathStr                        .matches("[^\\S]+(\\.avi|\\.mp4|\\.rmvb)$")) {                    listItem2.put("header", R.drawable.video);                } else if (pathStr.matches("[^\\S]+(\\.asv|\\.mp3)$")) {                    listItem2.put("header", R.drawable.audio);                } else if (pathStr                        .matches("[^\\S]+(\\.jpg|\\.png|\\.gif)$")) {                    listItem2.put("header", R.drawable.image);                } else {                    listItem2.put("header", R.drawable.doc);                }            }            listItem2.put("name", file.getName());            listItems.add(listItem2);        }        String[] from = {                "header", "name"        };        int[] to = {                R.id.header, R.id.name        };        SimpleAdapter adapter = new SimpleAdapter(this, listItems,                R.layout.file_row, from, to);        listview.setAdapter(adapter);    }    /**     * Inits the.     */    public void init() {        listview = (ListView) findViewById(R.id.file_listview);        mPath = (TextView) findViewById(R.id.current_path);        listview.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> arg0, View arg1,                    int position,                    long id) {                File file = new File(paths.get(position));                if (file.canRead()) {                    if (file.isDirectory()) {                        getFileDir(paths.get(position));                    }                    else {                        String data = "fail";                        try {                            FileInputStream stream = new FileInputStream(                                    paths.get(position));                            StringBuffer sb = new StringBuffer();                            int c;                            while ((c = stream.read()) != -1) {                                sb.append((char) c);                            }                            stream.close();                            data = sb.toString();                        } catch (Exception e) {                            // TODO: handle exception                            e.printStackTrace();                        }                        Intent i = getIntent();                        i.putExtra("data", data);                        setResult(RESULT_OK, i);                        finish();                    }                } else {                    Toast toast = Toast.makeText(OpenNote.this,                            "您的权限不足!", Toast.LENGTH_LONG);                    toast.show();                }            }        });    }    /* (non-Javadoc)     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)     */    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (keyCode == event.KEYCODE_BACK) {            if (currentpath == rootPath) {                File f = new File(currentpath);                getFileDir(currentpath);            }            else {                File f = new File(currentpath);                getFileDir(f.getParent());            }        }        return super.onKeyDown(keyCode, event);    }}



热点排行