「選択→背景色変更→OKボタンクリック→選択した処理の形式です」
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import yip.sample3.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* FileSelectChooser
*/
public class FileSelectChooser extends DialogFragment{
/* Bundle で渡す Key */
public final static String TITLE = "title";
public final static String ROOT_PATH = "root_path";
public final static String LISTENER = "Listner";
/* ファイル選択リスナ、Bundle で渡す */
public interface SelectListner extends Serializable{
public void onSelected(File file);
}
private static String mTitle;
static FileSelectChooser.SelectListner mListner;
static TextView mCurrentPathTextView;
static ListView mListView;
static List<File> mFileList;
static String mRootPath;
static String mCurrentPath;
static File mSelectFile;
final static Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg){
String path = msg.obj.toString();
mListView.setItemChecked(msg.arg1, false);
removeMessages(0);
mCurrentPathTextView.setText(path);
mCurrentPath = path;
mFileList.clear();
if (path.equals(mRootPath)==false) mFileList.add(new File(path+"/../"));
// FilesUtil.sort →前回投稿
for(File file:FilesUtil.sort(new File(path).listFiles())){
mFileList.add(file);
}
*1.notifyDataSetChanged();
}
};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState==null){
mTitle = getArguments().getString(TITLE);
mRootPath = getArguments().getString(ROOT_PATH, null);
mCurrentPath = mRootPath;
mFileList = new ArrayList<File>();
for(File f:FilesUtil.sort(new File(mRootPath).listFiles())){
mFileList.add(f);
}
}
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState){
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
mCurrentPathTextView = new TextView(getActivity());
mCurrentPathTextView.setBackgroundColor(0xffffffff);
mCurrentPathTextView.setTextColor(0xff000000);
mCurrentPathTextView.setPadding(8, 4, 8, 4);
mCurrentPathTextView.setText(mCurrentPath);
layout.addView(mCurrentPathTextView);
mListView = new ListView(getActivity());
mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
mListView.setAdapter(new ArrayAdapter<File>(getActivity(), 0, mFileList){
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public View getView(int position,View convertView,ViewGroup parent){
View view = convertView==null ? inflater.inflate(R.layout.filechooser_select_row, null) : convertView;
TextView tv = (TextView)view.findViewById(R.id.fileTextView);
File file = mFileList.get(position);
tv.setText(file.getName());
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
imageView.setImageResource(file.isDirectory() ? R.drawable.ic_menu_folder : R.drawable.ic_menu_file);
return view;
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
// 選択後の処理
BaseAdapter adapter = (BaseAdapter)parent.getAdapter();
File file = (File)adapter.getItem(position);
if (file.isDirectory()){
mSelectFile = null;
if (file.canRead()){
String path = file.getAbsolutePath();
if (path.endsWith("/..")){
path = file.getParentFile().getParent();
}
Message message = Message.obtain();
message.obj = path;
message.arg1 = position;
mHandler.sendMessage(message);
}else{
Toast.makeText(getActivity(), "permmision denied", Toast.LENGTH_SHORT).show();
mListView.setItemChecked(position, false);
}
}else{
if (file.canRead()){
mSelectFile = file;
}else{
Toast.makeText(getActivity(), "Can not read", Toast.LENGTH_SHORT).show();
mListView.setItemChecked(position, false);
}
}
}
});
layout.addView(mListView);
mListner = (SelectListner)getArguments().getSerializable(LISTENER);
return new AlertDialog.Builder(getActivity())
.setTitle(mTitle)
.setView(layout)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
if (mListner==null) return;
if (mSelectFile != null){
mListner.onSelected(mSelectFile);
}
}
}).create();
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return null;
}
}
=================================
(使用例)
以下のように、選択結果を格納する TextView と、選択リスナを宣言
static TextView mTextView;
static FileSelectChooser.SelectListner mSelectListner = new FileSelectChooser.SelectListner(){
@Override
public void onSelected(File file){
mTextView.setText(file.getAbsolutePath());
}
};
=== 実行 ===
Bundle args = new Bundle();
args.putString(FileSelectChooser.TITLE, "ファイル選択");
args.putString(FileSelectChooser.ROOT_PATH, "/mnt/sdcard");
args.putSerializable(FileSelectChooser.LISTENER, mSelectListner2);
DialogFragment df = new FileSelectChooser();
df.setArguments(args);
df.show(getFragmentManager(), "chooser");
*1:BaseAdapter)mListView.getAdapter(