Home»ソフトウェア»Android:ファイルパス入手のためのファイルリストの作成方法・その2

Android:ファイルパス入手のためのファイルリストの作成方法・その2

android_dev

前回の『Android:ファイルパス入手のためのファイルリストの作成方法・その1』では、ArrayAdapterクラスをそのまま使い、ファイルリストを表示しました。しかし、ArrayAdapterクラスは動的なもしくは細かい表示はできません。
なので今回はこのArrayAdapterクラスを拡張したクラスを作成し、アイコンとファイルサイズを表示するようにします。

概要

左は完成した画像
特徴としては
・ディレクトリかファイルかを判断し、ファイルにはファイルサイズを表示
・ディレクトリにはディレクトリ用のアイコンを適用
・BookShelfディレクトリには専用のアイコンを適用
・ファイルは拡張子でアイコンを変更(今回の例では.txt, .zip, .otf かそれ以外かを判断)

手順としては
①file_row.xmlの編集
②アイコンに使うファイルを投下
③FileListActivity.javaの編集
・ArrayAdapterを拡張したFileListAdapterクラスの作成
・CreateFileList関数の編集

②のアイコンに使うファイルを投下は説明しませんが、drawable-nodpiに入れておけばいいです。
今回使用したアイコンはこちらからお借りしました。
“ecqlipse 2″ PNG by ~chrfb on deviantART

今回紹介する下記の4ファイルについては上のタブから見ることができます。
  • layout/file_list.xml
  • layout/file_row.xml
  • values/style.xml
  • FileListActivity.java
filelist.xml
前回の『Android:ファイルパス入手のためのファイルリストの作成方法・その1』で使ったこのファイルはそのまま流用できます。
layout/file_list.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/DirName"
		style="@style/DirName" />
	<ListView
		android:id="@id/android:list"
		style="@style/FileList" />
</LinearLayout>

file_row.xml
①file_row.xmlの編集
file_row.xmlはfilelist.xmlで定義されているListView内のレイアウトを定義するファイルです。
従って、リスト内のアイコンや文字列はここで定義します。
layout/file_row.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
   	android:layout_width="fill_parent"
   	android:layout_height="fill_parent" >

		<ImageView
			android:id="@+id/List_Icon"
			style="@style/List_Icon" />

		<LinearLayout
			android:orientation="vertical"
   			android:layout_width="fill_parent"
   			android:layout_height="wrap_content" >

   			<TextView
    				android:id="@+id/List_File"
				style="@style/List_File" />

			<TextView
    				android:id="@+id/List_State"
				style="@style/List_State" />

		</LinearLayout>
</LinearLayout>

style.xml
styleは好きに書いていいですが、ここでの例を載せておきます。
また、前回まで使っていたidの「Row」は今回は使いません。
values/style.xml

<resource>
	~中略~
	<style name="FileList">
		<item name="android:layout_width">fill_parent</item>
		<item name="android:layout_height">wrap_content</item>
		<item name="android:scrollbars">vertical</item>
		<item name="android:layout_weight">1</item>
		<item name="android:clickable">true</item>
		<item name="android:focusableInTouchMode">true</item>
	</style>
	//ファイルアイコン
	<style name="List_Icon">
		<item name="android:layout_width">72px</item>
		<item name="android:layout_height">72px</item>
		<item name="android:background">@color/Black</item>
		<item name="android:paddingTop">0dip</item>
		<item name="android:paddingBottom">0dip</item>
		<item name="android:paddingLeft">0dip</item>
		<item name="android:paddingRight">0dip</item>
	</style>
	//ファイル名
	<style name="List_File">
		<item name="android:layout_width">fill_parent</item>
		<item name="android:layout_height">wrap_content</item>
		<item name="android:singleLine">true</item>
		<item name="android:textSize">18sp</item>
		<item name="android:textColor">@color/White</item>
		<item name="android:background">@color/Black</item>
		<item name="android:paddingTop">2dip</item>
		<item name="android:paddingBottom">0dip</item>
		<item name="android:paddingLeft">4dip</item>
		<item name="android:paddingRight">0dip</item>
	</style>
	//ファイルサイズ
	<style name="List_State">
		<item name="android:layout_width">fill_parent</item>
		<item name="android:layout_height">wrap_content</item>
		<item name="android:singleLine">true</item>
		<item name="android:textSize">14sp</item>
		<item name="android:textColor">@color/White</item>
		<item name="android:background">@color/Black</item>
		<item name="android:paddingTop">2dip</item>
		<item name="android:paddingBottom">0dip</item>
		<item name="android:paddingLeft">4dip</item>
		<item name="android:paddingRight">0dip</item>
	</style>
</resources>

FileListActivity.java
③FileListActivity.javaの編集
・ArrayAdapterを拡張したFileListAdapterクラスの作成
・CreateFileList関数の編集

// ファイルリスト構築関数
private void CreateFileList() {
	File[] files = new File(StrageDirPath).listFiles();
	if( files == null ) {
		Toast.makeText(FileListActivity.this, StrageDirPath + " は存在しない、もしくは開けません",
			       Toast.LENGTH_SHORT).show();
		return;
	}

	// カレントディレクトリ名をTextViewに設定
	TextView DirName = (TextView)findViewById(R.id.DirName);
	DirName.setText(StrageDirPath);

	if( items != null ) {
		items.clear();
	}

	if( items_size != null ) {
		items_size.clear();
	}

	items = new ArrayList<String>();
	items_size = new ArrayList<String>();

	// ルートじゃない場合は、階層を上がれるように".."をArrayListの先頭に設定
	if( !StrageDirPath.equals("/") ) {
		items.add("..");
		items_size.add(null);
	}

	// ファイル・ディレクトリがあるだけひたすらArrayListに追加する
	for( File file : files ) {
		if( file.isDirectory() ) {
			items.add(file.getName() + "/" );
			items_size.add(null);
		} else {

			items.add(file.getName());

			//ファイルサイズ取得と、小数第三位で四捨五入して単位変換
			double file_size;
			if ( file.length() >= 1000000000) {

				file_size = (double)file.length() / 1000000000;
				BigDecimal bi = new BigDecimal(String.valueOf(file_size));
				file_size = bi.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
				items_size.add(Double.toString(file_size) + " GB");

			} else if ( 1000000000 > file.length() &amp;&amp; file.length() >= 1000000) {

				file_size = (double)file.length() / 1000000;
				BigDecimal bi = new BigDecimal(String.valueOf(file_size));
				file_size = bi.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
				items_size.add(Double.toString(file_size) + " MB");

			} else if ( 1000000 > file.length() &amp;&amp; file.length() >= 1000) {

				file_size = (double)file.length() / 1000;
				BigDecimal bi = new BigDecimal(String.valueOf(file_size));
				file_size = bi.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
				items_size.add(Double.toString(file_size) + " KB");

			} else if ( 1000 > file.length() ) {

				items_size.add(Long.toString(file.length()) + " B");

			}
		}
	}

	// 拡張したクラスをListAdapterにセット
	setListAdapter(new FileListAdapter());
}

// ArrayAdapterを拡張し、独自リストを作成
class FileListAdapter extends ArrayAdapter {
	FileListAdapter() {
		super(FileListActivity.this, R.layout.file_row, items);
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		// Inflaterを使ってfile_row.xmlのテキストや画像をコードからセット
		LayoutInflater inflater = getLayoutInflater();
		View file_row = inflater.inflate(R.layout.file_row, parent, false);

		ImageView List_Icon = (ImageView)file_row.findViewById(R.id.List_Icon);
		TextView List_File = (TextView)file_row.findViewById(R.id.List_File);
		TextView List_State = (TextView)file_row.findViewById(R.id.List_State);

		String now_item;
		String now_item_size;
		now_item = (String)items.get(position);
		now_item_size = (String)items_size.get(position);

		// ファイルの種類等からアイコンやテキストを選択
		if ( now_item.equals("..") ) {
			List_Icon.setImageResource(R.drawable.power_restart);
		} else if ( now_item.substring( now_item.length() - 1 ).equals("/") ) {
			if( now_item.equals(value_other_bookshelfpath + "/") ){
				List_Icon.setImageResource(R.drawable.folder_documents);
			} else {
				List_Icon.setImageResource(R.drawable.folder);
			}

		} else if ( now_item.substring( now_item.length() - 4 ).equals(".txt") ) {
			List_Icon.setImageResource(R.drawable.file_text);
			List_State.setText(now_item_size);
		} else if ( now_item.substring( now_item.length() - 4 ).equals(".otf") ) {
			List_Icon.setImageResource(R.drawable.file_font_t);
			List_State.setText(now_item_size);
		} else if ( now_item.substring( now_item.length() - 4 ).equals(".zip") ) {
			List_Icon.setImageResource(R.drawable.file_zip);
			List_State.setText(now_item_size);
		} else {
			List_Icon.setImageResource(R.drawable.file);
			List_State.setText(now_item_size);
		}
		List_File.setText(now_item);
		return(file_row);

	}
}

このエントリーをはてなブックマークに追加
Bookmark this on Google Bookmarks
Post to Google Buzz
Bookmark this on FC2 Bookmark
Bookmark this on Yahoo Bookmark
Bookmark this on @nifty clip
Bookmark this on Livedoor Clip
email this

タグ:


3 Responses to "Android:ファイルパス入手のためのファイルリストの作成方法・その2"

  1. Hiro:

    はじめまして、Hiroと申します。
    リストビューのカスタマイズについて調べていて、こちらへたどり着きました。

    >リスト内のアイコンや文字列はここで定義します。
    との事でしたので、内容から恐らく[layout/file_row.xml]のソースコード表示がメインアクティビティの物になっていると思われます。

    勘違いでしたらすみません。

  2. はじめまして、KIYO*です。

    ご指摘いただいた点を確認したところ、[layout/file_row.xml]の中身が[layout/file_list.xml]のものとなっていたましたので、記事を修正しました。ありがとうございます。

    また何かありましたらコメント下さい。