Androidでディレクトリ選択プリファレンスを使う

理ろぐさんの記事を参考に作りました. http://relog.xii.jp/archives/2010/09/android_1.html


DirectorySelectDialog.java

package net.flaxia.android.githubviewer;

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

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class DirectorySelectDialog extends Activity implements DialogInterface.OnClickListener {
    private Context mContext;
    private ArrayList<File> mDirectoryList;
    private onDirectoryListDialogListener mListenner;

    public DirectorySelectDialog(Context context) {
        mContext = context;
        mDirectoryList = new ArrayList<File>();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if ((null != mDirectoryList) && (null != mListenner)) {
            File file = mDirectoryList.get(which);
            show(file.getAbsolutePath(), file.getPath());
        }
    }

    public void show(final String path, String title) {
        try {
            File[] mDirectories = new File(path).listFiles();
            if (null == mDirectories && null != mListenner) {
                mListenner.onClickFileList(null);
            } else {
                mDirectoryList.clear();
                ArrayList<String> viewList = new ArrayList<String>();
                for (File file : mDirectories) {
                    if (file.isDirectory()) {
                        viewList.add(file.getName() + "/");
                        mDirectoryList.add(file);
                    }
                }

                // ダイアログ表示
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
                alertDialogBuilder.setTitle(title);
                alertDialogBuilder.setItems(viewList.toArray(new String[0]), this);
                // 自身のContextではgetStringが失敗する
                alertDialogBuilder.setPositiveButton(mContext.getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                System.out.println(path);
                                mListenner.onClickFileList(path);
                            }
                        });
                // 自身のContextではgetStringが失敗する
                alertDialogBuilder.setNegativeButton(mContext.getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mListenner.onClickFileList(null);
                            }
                        });
                alertDialogBuilder.show();
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    public void setOnFileListDialogListener(onDirectoryListDialogListener listener) {
        mListenner = listener;
    }

    public interface onDirectoryListDialogListener {
        public void onClickFileList(String path);
    }
}

DirectorySelectDialogPreference.java

package net.flaxia.android.githubviewer;

import java.io.File;

import net.flaxia.android.githubviewer.DirectorySelectDialog.onDirectoryListDialogListener;
import net.flaxia.android.githubviewer.util.Configuration;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;

public class DirectorySelectDialogPreference extends DialogPreference implements
        DirectorySelectDialog.onDirectoryListDialogListener {

    public DirectorySelectDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View view) {
        SharedPreferences pref = getSharedPreferences();
        String summry = Configuration.DEFAULT_SAVE_PATH;
        if (null != pref) {
            summry = pref.getString(getKey(), summry);
        }
        setSummary(summry);
        super.onBindView(view);
    }

    @Override
    protected void onClick() {
        File externalStorage = Environment.getExternalStorageDirectory();
        DirectorySelectDialog dlg = new DirectorySelectDialog(getContext());
        dlg.setOnFileListDialogListener((onDirectoryListDialogListener) this);
        dlg.show(externalStorage.getAbsolutePath(), externalStorage.getPath());
    }

    @Override
    public void onClickFileList(String path) {
        if (null != path) {
            SharedPreferences.Editor editor = getEditor();
            editor.putString(getKey(), path);
            editor.commit();
            notifyChanged();
        }
    }
}

このように使います.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <net.flaxia.android.githubviewer.DirectorySelectDialogPreference
        android:key="saveDir" android:title="@string/directory_location_to_store" />
</PreferenceScreen>
This entry was posted in プログラミング.

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>