Android Smart Dict - 单词导入 - 文件浏览器
Android Smart Dict - 单词导入 - 文件浏览器
搞定了数据库之后,我要想办法把单词导入进来。怎么做呢?首先找到我要导入的文件,然后导入单词。
我做了一个简单的文件浏览器,可以去sdcard里面找我需要导入的文件。我需要自己定义一种词库的文件格式和后缀名,这个不重要,目前我只需要简单的定义就好,后面再做优化。后缀名就定为“.dict",那么首先需要搞定这个文件浏览器。当然我可以用Grid View来显示文件,就像home界面的桌面一样。用户点击文件夹后进入下一级文件目录,点击文件之后判断是否是".dict"结尾的文件,如果是就加载,否则不予处理,或者显示提示信息。
这里有一个重要的问题就是,我们在读取文件的时候不能在UI主线程里边做,这样有可能会阻塞进而导致讨厌的ANR被弹出来。我们可以自定义新的线程,当然也可以使用AsyncTask来做。我这里就用Android系统的AsyncTask来做,定义一个task名叫LoadFileInfoTask,主要就是用来读取文件,取得文件信息,封装成一个定义好的FileInfo类,里面只有三个属性,文件名,路径和图片,如果是我们的词库文件就显示词库图片,依次类推。最后要注意一点,execute这个AsyncTask需要在UI线程里边来做,否则会抛异常。
具体代码如下:
- private class LoadFileInfoTask extends AsyncTask<String, Void, Boolean>{
- private String path;
- private FileInfo[] list;
- @Override
- protected Boolean doInBackground(String... _path) {
- path = _path[0];
- final File file = new File(path);
- if(file.isFile()) {
- // Load book file
- if (path.endsWith(BOOK_FILE_SUFFIX)) {
- loadBook(file);
- }
- // It is not the book file
- else {
- FileLoaderActivity.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(FileLoaderActivity.this, R.string.toast_not_book_file,
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- return false;
- } else {
- list = getFileInfo(file);
- return true;
- }
- }
- @Override
- protected void onPostExecute(Boolean isPath) {
- if(isPath) {
- if(mFileAdapter == null) {
- mFileAdapter = new FileAdapter(list, path);
- mFileGrid.setAdapter(mFileAdapter);
- } else {
- mFileAdapter.updateData(list, path);
- }
- setTitle(path);
- }
- }
- private FileInfo[] getFileInfo(File directory) {
- File[] files;
- FileInfo[] list;
- files = directory.listFiles(new FileFilter() {
- public boolean accept(File file) {
- return file.canRead() && !file.isHidden();
- }
- });
- if(files == null) {
- return null;
- }
- list = new FileInfo[files.length];
- for (int i = 0; i < files.length; i++) {
- list[i] = new FileInfo();
- list[i].name = files[i].getName();
- String subPath = files[i].getAbsolutePath();
- // If it is a directory
- if (files[i].isDirectory()) {
- list[i].path = Utils.formatDirectory(subPath);
- list[i].icon = R.drawable.file_folder;
- }
- // If it is a file
- else {
- list[i].path = subPath;
- // Filter book files;
- if (list[i].name.endsWith(BOOK_FILE_SUFFIX)) {
- list[i].icon = R.drawable.file_book;
- } else {
- list[i].icon = R.drawable.file_unknown;
- }
- }
- }
- return list;
- }
- }
有了这个task,我们就可以在每次点击文件夹以后运行这个task取得下一级路径的文件信息。
然后,定义一个grid view,用来显示文件信息。还有相应的Adapter,每次显示的时候更新数据。
代码如下:
- /**
- * Adapter for file info grid view;
- */
- private class FileAdapter extends BaseAdapter {
- private FileInfo[] mList;
- private LayoutInflater inflater;
- public FileAdapter(FileInfo[] list, String path) {
- inflater = getLayoutInflater();
- updateData(list, path);
- }
- public int getCount() {
- return mList.length;
- }
- public Object getItem(int position) {
- return mList[position];
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = inflater.inflate(R.layout.file_grid_item, parent, false);
- }
- TextView text = (TextView) convertView.findViewById(R.id.label);
- FileInfo info = mList[position];
- text.setText(info.name);
- text.setCompoundDrawablesWithIntrinsicBounds(0, info.icon, 0, 0);
- text.setTag(info.path);
- return convertView;
- }
- public void updateData(final FileInfo[] list, final String path) {
- // If current path is not root, we have to show one more item for parent path.
- // Otherwise, do not;
- if(path.equals(PATH_ROOT)) {
- mList = list;
- } else {
- // If list is null just parent will be shown;
- if(list == null) {
- mList = new FileInfo[1];
- // Just one item for parent;
- mList[0] = new FileInfo(PATH_PARENT_NAME, R.drawable.file_folder,
- Utils.getParentPath(path));
- } else {
- final int length = list.length;
- mList = new FileInfo[length + 1];
- System.arraycopy(list, 0, mList, 0, length);
- // One more item for parent;
- mList[length] = new FileInfo(PATH_PARENT_NAME, R.drawable.file_folder,
- Utils.getParentPath(path));
- }
- }
- // Notify refresh itself;
- notifyDataSetChanged();
- }
- }
好了,今天主要的两个东西就是:
LoadFileInfoTask:主要用来取得文件信息。
GridView:主要用来显示文件信息,获得用户的点击之后判断是否显示下一级目录,或者加载文件。
来看看运行效果:
评论暂时关闭