2011/10/25 0 コメント

symfony2ブログ開設しました。

これからは、Symfony2に関してはSymfony2.GrAFRに書いてきます!
よろしくお願い致します。
2011/10/06 0 コメント

No input file specified.

Symfony2をさくらインターネットで動かそう!!とか思って phpのバージョンを 5.3.8 (CGI版) にした。
で、Symfony2 を動かした所、「No input file specified.」というエラーが。。。。

まぁ、エラーの内容はSymfony2とは関係ないんだけれど。。

これは、CGI版PHP環境でなんかのmoduleを使用する際に起こるらしい。
やっぱりVPSを契約してもらうべきだったか。。。と思ったけど、普通に解決できたので問題ないっぽい!!

ただ、php.iniに
cgi.fix_pathinfo = 1
を加えるだけでOKっぽいです。同時にタイムゾーンも指定しておきました。
date.timezone = Asia/Tokyo
2011/07/21 0 コメント

Cent OS (5.5) に MeCab をインストール

MeCab: Yet Another Part-of-Speech and Morphological Analyzer

MeCabで形態素解析!ってことで、CentOS (5.5) にさっそく MeCab0.98 を導入してみます。

① まず MeCabのインストール
[root@ ~]# wget 'http://downloads.sourceforge.net/project/mecab/mecab/0.98/mecab-0.98.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmecab%2Ffiles%2F&ts=1283097111&use_mirror=jaist'
[root@ ~]# tar xvfz mecab-0.98.tar.gz
[root@ ~]# cd mecab-0.98
[root@ ~]# ./configure
[root@ ~]# make
[root@ ~]# make install

② IPA 辞書をダウンロード
IPA 辞書, Juman 辞書 があるが、前者が推奨されていたので、それをダウンロードします。また, configureの際に文字コードを指定します(UTF-8とします)。
違う文字コードでインストールしてしまった場合は、make uninstall してからやり直せばOK
[root@ ~]# wget 'http://downloads.sourceforge.net/project/mecab/mecab-ipadic/2.7.0-20070801/mecab-ipadic-2.7.0-20070801.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fmecab%2Ffiles%2F&ts=1283097195&use_mirror=jaist'
[root@ ~]# tar xvfz mecab-ipadic-2.7.0-20070801.tar.gz
[root@ ~]# cd mecab-ipadic-2.7.0-20070801
[root@ ~]# ./configure --with-charset=utf8
[root@ ~]# make
[root@ ~]# make install


※ これで終了。最後に何か文字列(明日は明日の風が吹く)を解析してみます。

[root@ ~]# mecab
明日は明日の風が吹く
明日 名詞,副詞可能,*,*,*,*,明日,アシタ,アシタ
は 助詞,係助詞,*,*,*,*,は,ハ,ワ
明日 名詞,副詞可能,*,*,*,*,明日,アシタ,アシタ
の 助詞,連体化,*,*,*,*,の,ノ,ノ
風 名詞,一般,*,*,*,*,風,カゼ,カゼ
が 助詞,格助詞,一般,*,*,*,が,ガ,ガ
吹く 動詞,自立,*,*,五段・カ行イ音便,基本形,吹く,フク,フク
EOS


※ おまけとして、PHPからアクセスするために MeCab extension をインストールします。
http://d.hatena.ne.jp/yoshi-ken/20110621 の記事を参考にさせていただきました!
pear channel-discover pecl.opendogs.org
pear install opendogs/mecab-beta
echo "extension=mecab.so" > /etc/php.d/mecab.ini
php -m | fgrep mecab #インストールに成功していたら「mecab」という行が表示されます

  1. <?php  
  2. $target_str = "明日は明日の風が吹く";  
  3. $result = mecab_split($target_str);  
  4. print_r($result);  
  5. ?>  

これで Array ( [0] => 明日 [1] => は [2] => 明日 [3] => の [4] => 風 [5] => が [6] => 吹く ) となります。
2011/07/13 0 コメント

iPhone - json-framework

jsonのパースをしたかったので、ちょっと調べてみたら、json-frameworkというのがヨサゲ。
ってことで導入してみました。

json-frameworkは Google Code にあります。http://code.google.com/p/json-framework/

① GitHubからダウンロード! (v3.0.1),
② Classesの中身をプロジェクトにインポート!(自分は, JSONというグループを作成し、そこにインポートしました。)


③ 利用したいクラスの先頭で
  1. #import "SBJson.h"  
とすれば、OK。

④ あとは NSString として jsonデータを与えパースすれば、配列として返ってくるっぽい
NSArray jsonArray = [json_str JSONValue];


ってことで json-framework についての簡単なメモでした。
2011/07/12 0 コメント

iPhone - 開発入門② IBを使用しない場合

Interface Builder を使用しないでアプリケーションを作成する場合は, 最初に以下の手順を行い, IBを使用しない設定に変更する。

① Window-based Application でプロジェクトを作成する。(プロジェクト名はHogeとする)

② Resouces 内の xibファイルである「MainWindow.xib」を削除する。

③ Resouces 内の「Hoge-Info.plist」を開き, 一番下の「Main nib file base name」のプロパティを削除

④ Other Sources 内の「main.m」を開き、以下の様変更を加える。(通常は, xibファイルがDelegateクラスを呼び出していたが、それを削除しため指定してやる必要がある。)

< 変更前 >
  1. int main(int argc, char *argv[]) {  
  2.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  3.     int retVal = UIApplicationMain(argc, argv, nil, nil);  
  4.     [pool release];  
  5.     return retVal;  
  6. }  

< 変更後 >
  1. int main(int argc, char *argv[]) {  
  2.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  3.     int retVal = UIApplicationMain(argc, argv, nil, @"HogeAppDelegate");  
  4.     [pool release];  
  5.     return retVal;  
  6. }  


⑤ Other Sources 内 のファイルで, @property や @ synthesize を削除してしまう。

_________________________________________________________

これで設定は完了です。あとは, HogeAppDelegateで自分でwindowの作成をし、viewをつくっていく感じ。

  1. // HogeAppDelegate.m  
  2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  3.       
  4.     // windowを作成  
  5.     CGRect screenbounds = [[UIScreen mainScreen] bounds];  
  6.     window = [[UIWindow alloc] initWithFrame:screenbounds];  
  7.   
  8.     /** 
  9.      * ここでwindow上にaddSubviewしていけば良い 
  10.      */  
  11.   
  12.     [window makeKeyAndVisible];  
  13.   
  14.     return YES;  
  15. }  
2011/07/12 0 コメント

iPhone - 開発入門① テンプレートの選択

※Xcodeのインストール等の環境構築の説明は行いません。

まず, Xcodeを起動して, テンプレートタイプを選ぶ。(以下の画面)



まず、どのテンプレートを使用すればいいかで困惑するはずです。
テンプレートは以下の物

  • Navigation-based Application
    リストの項目を選択したら、その項目について階層的に潜っていくような階層構造を持ったタイプ。
  • OpenGL ES Application
    3Dのグラフィックを使用したゲームなどのようなOpenGLを使用したタイプ
  • Split View-based Application
    iPadでSplit Viewを使うようなタイプ
  • Tab Bar Application
    下部にTabを生成しウィンドウを切り替えるタイプ
  • Utility Application
    iPhoneの天気アプリのような、フリップして裏画面で設定を行うようなタイプ
  • View-based Application
    ビューを1つだけ使用するタイプ
  • Window-based Application
    全ての基礎となるタイプ

しかし、音楽のための iPodアプリケーションのように、タブがあってナビゲーションもあるアプリを作りたい場合、どうすればよいか?という疑問が浮かぶはずです!

この場合は, Tab Bar Application の タブの1つとして Navigation-based のビューを生成すれば良い。


※ 基本的にInterface Builder(以下IB)を利用して画面を生成していくのですが、その方法だと裏側がどうなっているのかが理解できず、タブがあってナビゲーションもあるアプリのように複雑なものは理解しずらいのでは?と思います。そこで、IBを使用しないで作成する方法で記事を書いていきます。(個人的にIBはよくわからないっていうのが本音なんですけどね。笑) そのため、Window-based Application を使用していきます

次回は、その前提としてアプリケーションの画面の構成について自分なりにまとめてみます。
2011/05/30 0 コメント

Android - SQLiteでの複数レコード挿入

複数のレコードを追加するためのメソッドを作成しました。
conflictAlgorithm には, SQLiteDatabase.CONFLICT_NONE, SQLiteDatabase.CONFLICT_REPLACE などの, コンフリクトが発生した場合の処理を指定します。
また, transaction を true にした場合は, トランザクション処理を併用します。

  1. /** 
  2.  * insert a lot of data 
  3.  * 
  4.  * @param nullColumnHack  
  5.  * @param valueList 値のリスト 
  6.  * @param conflictAlgorithm コンフリクト発生時の処理 
  7.  * @param transaction トランザクション処理を併用するか否か 
  8.  * @return Boolean 成功 or 失敗 
  9.  */  
  10. public Boolean insertMany(SQLiteDatabase db , String table , String nullColumnHack, List<ContentValues> valueList, int conflictAlgorithm, Boolean transaction) {  
  11.       
  12.     if(valueList != null && valueList.size() > 0){  
  13.         String[] CONFLICT_VALUES = new String[]{""" OR ROLLBACK "" OR ABORT "" OR FAIL "" OR IGNORE "" OR REPLACE "};  
  14.   
  15.         // At first, create sql statement  
  16.         ContentValues initialValues = valueList.get(0);  
  17.           
  18.         // Measurements show most sql lengths <= 152  
  19.         StringBuilder sql_build = new StringBuilder(152);  
  20.         sql_build.append("INSERT");  
  21.         sql_build.append(CONFLICT_VALUES[conflictAlgorithm]);  
  22.         sql_build.append(" INTO ");  
  23.         sql_build.append(table);  
  24.         // Measurements show most values lengths < 40  
  25.         StringBuilder values = new StringBuilder(40);  
  26.   
  27.         Set<Map.Entry<String, Object>> entrySet = null;  
  28.           
  29.         if (initialValues != null && initialValues.size() > 0) {  
  30.             entrySet = initialValues.valueSet();  
  31.             Iterator<Map.Entry<String, Object>> entriesIter = entrySet.iterator();  
  32.             sql_build.append('(');  
  33.   
  34.             boolean needSeparator = false;  
  35.             while (entriesIter.hasNext()) {  
  36.                 if (needSeparator) {  
  37.                     sql_build.append(", ");  
  38.                     values.append(", ");  
  39.                 }  
  40.                 needSeparator = true;  
  41.                 Map.Entry<String, Object> entry = entriesIter.next();  
  42.                 sql_build.append(entry.getKey());  
  43.                 values.append('?');  
  44.             }  
  45.             sql_build.append(')');  
  46.         } else {  
  47.             sql_build.append("(" + nullColumnHack + ") ");  
  48.             values.append("NULL");  
  49.         }  
  50.   
  51.         sql_build.append(" VALUES(");  
  52.         sql_build.append(values);  
  53.         sql_build.append(");");  
  54.         String sql = sql_build.toString();  
  55.           
  56.         SQLiteStatement statement = null;  
  57.           
  58.         // if transaction id true, beginTransaction()  
  59.         if(transaction){  
  60.             db.beginTransaction();  
  61.         }  
  62.         try {  
  63.             for(int i=0,length=valueList.size(); i<length; i++){  
  64.                 statement = db.compileStatement(sql);  
  65.                 initialValues = valueList.get(i);  
  66.                 entrySet = initialValues.valueSet();  
  67.                   
  68.                 // Bind the values  
  69.                 if (entrySet != null) {  
  70.                     int size = entrySet.size();  
  71.                     Iterator<Map.Entry<String, Object>> entriesIter = entrySet.iterator();  
  72.                     for (int j = 0; j < size; j++) {  
  73.                         Map.Entry<String, Object> entry = entriesIter.next();  
  74.                         DatabaseUtils.bindObjectToProgram(statement, j + 1, entry.getValue());  
  75.                     }  
  76.                 }  
  77.                 // Run the program and then cleanup  
  78.                 statement.execute();  
  79.                 statement.close();  
  80.             }  
  81.               
  82.             // if transaction id true, setTransactionSuccessful()  
  83.             if(transaction){  
  84.                 db.setTransactionSuccessful();  
  85.                 db.endTransaction();  
  86.                 transaction = false;  
  87.             }  
  88.             return true;  
  89.         } catch (SQLiteDatabaseCorruptException e) {  
  90.             throw e;  
  91.         } finally {  
  92.             if (statement != null) {  
  93.                 statement.close();  
  94.             }  
  95.             // if transaction id true, endTransaction()  
  96.             if(transaction){  
  97.                 db.endTransaction();  
  98.             }  
  99.         }  
  100.     }  
  101.     return false;  
  102. }  
2011/05/10 0 コメント

Android - SQLite 文字列ソート

SQLiteの文字列ソートにおいて (SQLiteに限らず) , かな, 英字, 数字, 記号 の順番を群単位で変えたい場合は, そのためのフィールドを追加するのが一番簡単っぽい。というより、これしか思い浮かばなかった。

デフォルトだと<数字, 英字 .... >という順番だったが, ワードの頭文字に対して<かな, 記号(その他), 数字, 英語> という順番にソートしたかった。そこで, sortgroupというフィールドを目的のテーブルに作成する。

sortgroupの値は, 半角カナ → 1, 記号(その他) → 2, 数字 → 3, 英字 → 4 とする。

こうすれば, SELECT * FROM table ORDER BY sortgroup ASC みたいにすれば取ってこれるよね。

で、頭文字がどのタイプのものかを判別するためのプログラムは以下

  1. public class CheckWordType {  
  2.     // 文字列が 半角カタカナ(1), その他(2), 数字(3), アルファベット(4)のどれかを調べる  
  3.     public int checkCharType(char c){  
  4.         if(c >= 0xff66 && c <= 0xff9d){ // 半角カタカナ (1) http://ash.jp/code/unitbl1.htm 参照  
  5.             return 1;  
  6.         }else if(Character.isLetter(c)){ // アルファベット (4)  
  7.             return 4;  
  8.         }else if(Character.isDigit(c)){ // 数字 (3)  
  9.             return 3;  
  10.         }else// その他 (2)  
  11.             return 2;  
  12.         }  
  13.     }  
  14. }  

まぁ、まとめるほどの事でもないんだけど、書く記事がなかったんでw
2011/04/27 0 コメント

Android - プログラムでLinearLayoutのGravity属性を設定

LinearLayoutそのものを中央寄せしたい場合は, 以下のように LayoutParams の gravity 変数に値を設定すれば良い。

  1. LinearLayout.LayoutParams lp =  
  2.     new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,  
  3.                                   LinearLayout.LayoutParams.WRAP_CONTENT);  
  4. lp.gravity = Gravity.CENTER_HORIZONTAL;   
  5. view.setLayoutParams(lp);  


view.setGravity(gravity); で設定しようとしてはまったのでメモ。
LinearLayout の小ビューの中央寄せはをする場合に, view.setGravity(gravity); を使うのかな?
2011/04/27 0 コメント

Android - 画面の横幅と縦幅の取得

画面の横幅と縦幅を取得したい場合は以下の様にすれば良い

  1. DisplayMetrics metrics = new DisplayMetrics();  
  2. getWindowManager().getDefaultDisplay().getMetrics(metrics);  
  3. int width = metrics.widthPixels;  // 横幅サイズを取得  
  4. int height = metrics.heightPixels;  // 縦幅サイズを取得  

ちなみに, 幅が小さいほうが横幅, 大きいほうが縦幅というわけではなく, その時の画面の向きに対する横幅・縦幅です。
2011/04/20 0 コメント

MINE TYPE の一覧 (cakePHPより)

cakePHP1.3のmedia.phpで定義されているMINEタイプの一覧は以下のものとなっていた。
しっておくと便利かもしれないので、一応メモ!

  1. var $mimeType = array(  
  2.     'ai' => 'application/postscript',  
  3.     'bcpio' => 'application/x-bcpio',  
  4.     'bin' => 'application/octet-stream',  
  5.     'ccad' => 'application/clariscad',  
  6.     'cdf' => 'application/x-netcdf',  
  7.     'class' => 'application/octet-stream',  
  8.     'cpio' => 'application/x-cpio',  
  9.     'cpt' => 'application/mac-compactpro',  
  10.     'csh' => 'application/x-csh',  
  11.     'csv' => 'application/csv',  
  12.     'dcr' => 'application/x-director',  
  13.     'dir' => 'application/x-director',  
  14.     'dms' => 'application/octet-stream',  
  15.     'doc' => 'application/msword',  
  16.     'drw' => 'application/drafting',  
  17.     'dvi' => 'application/x-dvi',  
  18.     'dwg' => 'application/acad',  
  19.     'dxf' => 'application/dxf',  
  20.     'dxr' => 'application/x-director',  
  21.     'eot' => 'application/vnd.ms-fontobject',  
  22.     'eps' => 'application/postscript',  
  23.     'exe' => 'application/octet-stream',  
  24.     'ez' => 'application/andrew-inset',  
  25.     'flv' => 'video/x-flv',  
  26.     'gtar' => 'application/x-gtar',  
  27.     'gz' => 'application/x-gzip',  
  28.     'bz2' => 'application/x-bzip',  
  29.     '7z' => 'application/x-7z-compressed',  
  30.     'hdf' => 'application/x-hdf',  
  31.     'hqx' => 'application/mac-binhex40',  
  32.     'ico' => 'image/vnd.microsoft.icon',  
  33.     'ips' => 'application/x-ipscript',  
  34.     'ipx' => 'application/x-ipix',  
  35.     'js' => 'application/x-javascript',  
  36.     'latex' => 'application/x-latex',  
  37.     'lha' => 'application/octet-stream',  
  38.     'lsp' => 'application/x-lisp',  
  39.     'lzh' => 'application/octet-stream',  
  40.     'man' => 'application/x-troff-man',  
  41.     'me' => 'application/x-troff-me',  
  42.     'mif' => 'application/vnd.mif',  
  43.     'ms' => 'application/x-troff-ms',  
  44.     'nc' => 'application/x-netcdf',  
  45.     'oda' => 'application/oda',  
  46.     'otf' => 'font/otf',  
  47.     'pdf' => 'application/pdf',  
  48.     'pgn' => 'application/x-chess-pgn',  
  49.     'pot' => 'application/mspowerpoint',  
  50.     'pps' => 'application/mspowerpoint',  
  51.     'ppt' => 'application/mspowerpoint',  
  52.     'ppz' => 'application/mspowerpoint',  
  53.     'pre' => 'application/x-freelance',  
  54.     'prt' => 'application/pro_eng',  
  55.     'ps' => 'application/postscript',  
  56.     'roff' => 'application/x-troff',  
  57.     'scm' => 'application/x-lotusscreencam',  
  58.     'set' => 'application/set',  
  59.     'sh' => 'application/x-sh',  
  60.     'shar' => 'application/x-shar',  
  61.     'sit' => 'application/x-stuffit',  
  62.     'skd' => 'application/x-koan',  
  63.     'skm' => 'application/x-koan',  
  64.     'skp' => 'application/x-koan',  
  65.     'skt' => 'application/x-koan',  
  66.     'smi' => 'application/smil',  
  67.     'smil' => 'application/smil',  
  68.     'sol' => 'application/solids',  
  69.     'spl' => 'application/x-futuresplash',  
  70.     'src' => 'application/x-wais-source',  
  71.     'step' => 'application/STEP',  
  72.     'stl' => 'application/SLA',  
  73.     'stp' => 'application/STEP',  
  74.     'sv4cpio' => 'application/x-sv4cpio',  
  75.     'sv4crc' => 'application/x-sv4crc',  
  76.     'svg' => 'image/svg+xml',  
  77.     'svgz' => 'image/svg+xml',  
  78.     'swf' => 'application/x-shockwave-flash',  
  79.     't' => 'application/x-troff',  
  80.     'tar' => 'application/x-tar',  
  81.     'tcl' => 'application/x-tcl',  
  82.     'tex' => 'application/x-tex',  
  83.     'texi' => 'application/x-texinfo',  
  84.     'texinfo' => 'application/x-texinfo',  
  85.     'tr' => 'application/x-troff',  
  86.     'tsp' => 'application/dsptype',  
  87.     'ttf' => 'font/ttf',  
  88.     'unv' => 'application/i-deas',  
  89.     'ustar' => 'application/x-ustar',  
  90.     'vcd' => 'application/x-cdlink',  
  91.     'vda' => 'application/vda',  
  92.     'xlc' => 'application/vnd.ms-excel',  
  93.     'xll' => 'application/vnd.ms-excel',  
  94.     'xlm' => 'application/vnd.ms-excel',  
  95.     'xls' => 'application/vnd.ms-excel',  
  96.     'xlw' => 'application/vnd.ms-excel',  
  97.     'zip' => 'application/zip',  
  98.     'aif' => 'audio/x-aiff',  
  99.     'aifc' => 'audio/x-aiff',  
  100.     'aiff' => 'audio/x-aiff',  
  101.     'au' => 'audio/basic',  
  102.     'kar' => 'audio/midi',  
  103.     'mid' => 'audio/midi',  
  104.     'midi' => 'audio/midi',  
  105.     'mp2' => 'audio/mpeg',  
  106.     'mp3' => 'audio/mpeg',  
  107.     'mpga' => 'audio/mpeg',  
  108.     'ra' => 'audio/x-realaudio',  
  109.     'ram' => 'audio/x-pn-realaudio',  
  110.     'rm' => 'audio/x-pn-realaudio',  
  111.     'rpm' => 'audio/x-pn-realaudio-plugin',  
  112.     'snd' => 'audio/basic',  
  113.     'tsi' => 'audio/TSP-audio',  
  114.     'wav' => 'audio/x-wav',  
  115.     'asc' => 'text/plain',  
  116.     'c' => 'text/plain',  
  117.     'cc' => 'text/plain',  
  118.     'css' => 'text/css',  
  119.     'etx' => 'text/x-setext',  
  120.     'f' => 'text/plain',  
  121.     'f90' => 'text/plain',  
  122.     'h' => 'text/plain',  
  123.     'hh' => 'text/plain',  
  124.     'htm' => 'text/html',  
  125.     'html' => 'text/html',  
  126.     'm' => 'text/plain',  
  127.     'rtf' => 'text/rtf',  
  128.     'rtx' => 'text/richtext',  
  129.     'sgm' => 'text/sgml',  
  130.     'sgml' => 'text/sgml',  
  131.     'tsv' => 'text/tab-separated-values',  
  132.     'tpl' => 'text/template',  
  133.     'txt' => 'text/plain',  
  134.     'xml' => 'text/xml',  
  135.     'avi' => 'video/x-msvideo',  
  136.     'fli' => 'video/x-fli',  
  137.     'mov' => 'video/quicktime',  
  138.     'movie' => 'video/x-sgi-movie',  
  139.     'mpe' => 'video/mpeg',  
  140.     'mpeg' => 'video/mpeg',  
  141.     'mpg' => 'video/mpeg',  
  142.     'qt' => 'video/quicktime',  
  143.     'viv' => 'video/vnd.vivo',  
  144.     'vivo' => 'video/vnd.vivo',  
  145.     'gif' => 'image/gif',  
  146.     'ief' => 'image/ief',  
  147.     'jpe' => 'image/jpeg',  
  148.     'jpeg' => 'image/jpeg',  
  149.     'jpg' => 'image/jpeg',  
  150.     'pbm' => 'image/x-portable-bitmap',  
  151.     'pgm' => 'image/x-portable-graymap',  
  152.     'png' => 'image/png',  
  153.     'pnm' => 'image/x-portable-anymap',  
  154.     'ppm' => 'image/x-portable-pixmap',  
  155.     'ras' => 'image/cmu-raster',  
  156.     'rgb' => 'image/x-rgb',  
  157.     'tif' => 'image/tiff',  
  158.     'tiff' => 'image/tiff',  
  159.     'xbm' => 'image/x-xbitmap',  
  160.     'xpm' => 'image/x-xpixmap',  
  161.     'xwd' => 'image/x-xwindowdump',  
  162.     'ice' => 'x-conference/x-cooltalk',  
  163.     'iges' => 'model/iges',  
  164.     'igs' => 'model/iges',  
  165.     'mesh' => 'model/mesh',  
  166.     'msh' => 'model/mesh',  
  167.     'silo' => 'model/mesh',  
  168.     'vrml' => 'model/vrml',  
  169.     'wrl' => 'model/vrml',  
  170.     'mime' => 'www/mime',  
  171.     'pdb' => 'chemical/x-pdb',  
  172.     'xyz' => 'chemical/x-pdb'  
  173. );  
2011/04/18 0 コメント

Android - HTTP-POST通信を行う

HTTPのPOST処理を行う場合は, 以下のようにすればOK.

  1. /** 
  2.  * HTTP POSTを行う 
  3.  * 
  4.  * @param url HTTP通信を行うターゲットのURL 
  5.  * @param requestParams パラメータ 
  6.  * @param encode HTTP要求時の文字コード 
  7.  * @return 受信結果の文字列 
  8.  */  
  9. public String httpPostRequest( String url, Map<String, String> requestParams, String encode ){  
  10.  try{          
  11.   HttpPost httppost = new HttpPost( url );  
  12.      DefaultHttpClient client = new DefaultHttpClient();  
  13.    
  14.      // リクエストパラメータの設定  
  15.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  16.         for (Map.Entry<String, String> entry : requestParams.entrySet()) {  
  17.             params.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue()));  
  18.         }  
  19.    
  20.      // POST データの設定  
  21.      httppost.setEntity(new UrlEncodedFormEntity(params, encode));  
  22.   
  23.      HttpResponse response = client.execute( httppost );  
  24.      int status = response.getStatusLine().getStatusCode();  
  25.    
  26.      // 結果が正しく帰って来なければエラー  
  27.      if ( status != HttpStatus.SC_OK ){ throw new Exception( "" ); }  
  28.    
  29.      return EntityUtils.toString( response.getEntity());  
  30.  }catch ( Exception e ){  
  31.   return null;  
  32.  }  
  33. }  


POST データを付加する際,
httppost.setEntity(new UrlEncodedFormEntity(params, encode));
とするが, ここで第二引数に, ちゃんと encode を指定してあげること。
内部エンコードがUTF-8だろうからって指定しなかったために、えらい目に会いました。

Webプログラム (PHP等) でもそうだけど、文字コードは明示的にちゃんと指定してあげなきゃだめですね。猛省。。。
2011/04/07 0 コメント

動作しているサービスの一覧について

動作しているサービスの一覧は,


chkconfig --list というコマンドで把握できる。出力例は以下



NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off
acpid           0:off 1:off 2:on 3:on 4:on 5:on 6:off
anacron         0:off 1:off 2:off 3:off 4:off 5:off 6:off
atd             0:off 1:off 2:off 3:on 4:on 5:on 6:off
auditd         0:off 1:off 2:off 3:off 4:off 5:off 6:off
     :
     :
wpa_supplicant 0:off 1:off 2:off 3:off 4:off 5:off 6:off
xfs             0:off 1:off 2:off 3:off 4:off 5:off 6:off
ypbind         0:off 1:off 2:off 3:off 4:off 5:off 6:off
yum-updatesd   0:off 1:off 2:off 3:off 4:off 5:off 6:off


ここで 0 ~ 6 の数字はランレベルを表しており,


0 : システムの停止
1 : シングルユーザーモード
2 : マルチユーザーモード(NFSなし)
3 : マルチユーザーモード(テキストログイン)
4 : 未使用
5 : マルチユーザーモード(グラフィカルログイン)
6 : システムの再起動

である。ちなみに,  ランレベルについては こちら

2011/04/05 0 コメント

Android - 画面の向きの固定方法

画面の向きを固定したい!という場合があると思います。
その場合は, 以下の様にすれば良い。

  1. // 画面の向きを固定  
  2. public void fixDisplayOrientation(){  
  3.     Configuration config = getResources().getConfiguration();  
  4.     if(config.orientation == Configuration.ORIENTATION_PORTRAIT){  
  5.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  6.     }else if (config.orientation == Configuration.ORIENTATION_LANDSCAPE){  
  7.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  8.     }  
  9. }  
  10.   
  11. // 画面の向きの固定を解除  
  12. public void unfixDisplayOrientation(){  
  13.     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);  
  14. }  

ここで, 画面の向きを取得するとき, config.orientation の値を使用。
Configuration.ORIENTATION_PORTRAIT なのか Configuration.ORIENTATION_LANDSCAPE なのかで判断する。


しかし, 画面の向きを設定するとき, は, setRequestedOrientation(int orientation) に与える引数の値は, Configuration.ORIENTATION_PORTRAIT や Configuration.ORIENTATION_LANDSCAPE ではなく, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT または ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE なので注意!
2011/04/04 0 コメント

Android - IMEの表示/非表示

IMEをプログラムで表示/非表示の切り替えを行いたかったので、その時のメモ。

まずは表示方法から
  1. // 表示方法  
  2. EditText et = (EditText)findViewById(R.id.edittext);  
  3.   
  4. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  5. imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);  

次に非表示にする方法
  1. // 非表示方法  
  2. imm.hideSoftInputFromWindow(v.getWindowToken(),0);  
  3. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  

とりあえずこれで出来ました。
2011/04/01 0 コメント

Android - ローディング画像を表示する

画像読み込み中にローディング画像を表示させたい (クルクル回るやつ)。
この場合, Androidに用意されている ProgressBar を使用すれば良い。(gifアニメーションで実現しようとする場合は, 結構めんどくさそう)

  1. <ProgressBar  
  2.     android:id="@+id/product_image_loading"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     style="?android:attr/progressBarStyle"/>  

これだけでOK。

以下は例, 上から順に, styleは
progressBarStyleSmall
progressBarStyle
progressBarStyleLarge
progressBarStyleHorizontal
です。progressBarStyleHorizontal には, android:indeterminate="true" も指定してある。

2011/03/30 0 コメント

Android - colors.xml について

res/values/ に, colors.xmlというファイルを作成する (以下はその例)。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="color1">#ff66cdaa</color>  
  4.     <color name="color2">#ffff89c4</color>  
  5.     <color name="color3">#ffAAAAAA</color>  
  6. </resources>  

こうすることで, 独自の色を定義することができる。

xml から使用する場合は, android:background="@color/color1" などというように, @color/ で指定して上げれば良い。

プログラム中から使用する場合は, 例として
int color = getResources().getColor(R.color.color1);
view.setBackgroundColor(color); としてあげればOK。


view.setBackgroundColor(R.color.color1)という指定をしてしまってハマったのでメモメモ。
2011/03/29 0 コメント

Android - Activity 起動時にキーボードを表示させない

Activity を起動する際に, EditText にフォーカスが当たっている場合, キーボードが自動で表示される。
これを辞めたい場合 (キーボードの表示を禁止する場合),

setContentView を行う前に,
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
をしてあげれば良い。(※ android.view.WindowManager.LayoutParams をimport)

こちらを参考にさせて頂きました。

また, EditTextかたフォーカスが外れたらキーボードを隠したい場合は,

  1. EditText editText = (EditText)findViewById(R.id.EditText01);  
  2. editText.setOnFocusChangeListener(new View.OnFocusChangeListener(){  
  3.   
  4.     @Override  
  5.     public void onFocusChange(View v, boolean flag){  
  6.         if(flag == false){  
  7.             InputMethodManager inputMethodManager =  
  8.                 (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  9.             inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),0);  
  10.         }  
  11.     }  
  12. });  

というように, EditText の setOnFocusChange に inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),0)
を行わせてあげれば良い。
2011/03/25 0 コメント

Android - startActivityForResult & onActivityResult

メインアクティビティからサブアクティビティを呼び出し, サブアクティビティが終了した際にコールバックを呼ぶ。
ちょっとはまったのでメモ ( . .)Φ

このような事を行う際には,
① メインアクティビティ側で startActivityForResult を使用して サブアクティビティ を起動する。
② サブアクティビティで setResult(...) を使用して, 呼び出し側(メイン) に返す内容を定める
③ メインアクティビティ側の onActivityResult がコールバックで呼び出される。

という流れである。しかし, 呼び出し側 (メイン) と呼び出される側 (サブ) を両方共マニフェストファイルで SingleTask に設定していた場合, これが上手くいかなかった。。

この場合, 呼び出される (サブアクティビティ) 側には SingleTask や SingleInstance は設定しないほうが良いだろうという結論。
2011/03/24 0 コメント

Android - Examination of "Context"

Android では,

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
Intent intent = new Intent(MainActivity.this, HogeActivity.class);

といったように, 引数に Context を渡すことが頻繁にあります。(上でいう MainActivity.this)
この Context に関して, どういうものかを知っておくべきだと思い、自分なりにまとめてみる。


まず知っておくべき事としては

① Context は, アプリケーションのグローバルな環境情報を受け渡すために使用される (コンストラクタの引数によく与えられる)。 (これにより, 特定のアプリケーションのリソースやクラスにアクセスでき, アクティビティ起動やインテントのブロードキャスト, インテント受信といった操作を呼び出せる)

② Context には, Application ContextActivity Context というものが存在する。前者は, getApplicationContext() で取得する Context であり, 後者は上記の例のように 「this」で取得するものである。
後者の「this」は Activity である (Activity クラスは Context を継承している) 。

Application Context はアプリケーションに結び付けられていて, アプリケーションのライフサイクルで同じものである。これに対して, Activity Contextは Activity に結び付けられている ( 画面回転等の様な際に何度も破棄される可能性がある Activity )


では, Application ContextActivity Context はどう使い分ければ良いのか?
それに関するヒントとして,
http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
にメモリリークとの関係性が記述されているので, 参考にしておくと良いと思う。

様々な記事があるが、どれも言ってることは,

① Activity と同じライフサイクルの際には Activity Context を使用する。GUIを扱う際には特に注意すること。
② ライフサイクルが異なる場合に Activity Context を使用すると, メモリリークする可能性がある。その場合はApplication Context を使用するとよい。

ということなのかな?
正直, どちらの Context を使用すればいいか?という答えは難しく, 場合によってって感じでした(すみません。。。)。
なので, 経験を踏んで ライフサイクル を考えながら選ぶべきなんですね。

強いてゆうなれば, Activity のライフサイクルが終了した際に, 同時に破棄したい場合は Activity Context , それ以外やどちらを使用すればよくわかりません..って場合は, Application Context を使用するのが無難なのかな。。って感じです。
2011/03/21 0 コメント

Android - ログアウト処理の実装

前回の記事 (ログイン & ログアウト処理 (スタックの問題点)) で, ログイン & ログアウト処理の実装ではまった内容を説明した。今回はその解決法を説明する。

前回の記事における HogeActivity の内容を以下に変更する。(LoginActivityにもnohistory属性を付加)

  1. package com.dev_grafr.app.logintest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.content.SharedPreferences;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12.   
  13. public class HogeActivity extends Activity {  
  14.     public static final String PREFERENCES_FILE_NAME = "preference";  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.hoge);  
  20.         ((Button) findViewById(R.id.logoutBtn)).setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View v) {  
  23.                 logout();  
  24.                 Intent broadcastIntent = new Intent();  
  25.                 broadcastIntent.setAction("com.dev_grafr.app.logintest.LOGOUT");  
  26.                 sendBroadcast(broadcastIntent);  
  27.                 Intent intent = new Intent(getApplicationContext(), LoginActivity.class);  
  28.                 startActivity(intent);  
  29.             }  
  30.         });  
  31.          
  32.         IntentFilter intentFilter = new IntentFilter();  
  33.         intentFilter.addAction("com.dev_grafr.app.logintest.LOGOUT");  
  34.         registerReceiver(new BroadcastReceiver() {  
  35.             @Override  
  36.             public void onReceive(Context context, Intent intent) {  
  37.                 finish();  
  38.             }  
  39.         }, intentFilter);  
  40.     }  
  41.   
  42.     // ログアウト処理  
  43.     public void logout(){  
  44.         SharedPreferences settings = getSharedPreferences(PREFERENCES_FILE_NAME, 0); // 0 -> MODE_PRIVATE  
  45.         SharedPreferences.Editor editor = settings.edit();  
  46.         editor.putLong("logged-in"0);  
  47.         editor.commit();  
  48.     }  
  49. }  

これでOK

つまり, ログアウトしましたよ〜。というメッセージをスタック内の Activity に対して BroadCast してあげれば良い。
この BroadCast を受け取ったアクティビティは, 自分自身をfinishする。これで一度にスタックからアクティビティを除去できるわけである。


どうでしょう?なにか他にも ログイン & ログアウト処理 の実装方法があれば, 教えてくださると嬉しいです。
2011/03/21 0 コメント

Android - ログイン & ログアウト処理 (スタックの問題点)

ログイン & ログアウト処理があるアプリケーションを作成する際にはまった内容をメモ

最初にはまった内容を、説明します。
問題点とかどうでもいいから、ログイン & ログアウト処理 の方法だけ教えてくれればいいんだよ!って方は, ログアウト処理の実装の方を参照してください

流れとして,
① MainActivity においてログイン済みかどうかの判定を行う (MainAvtivity は, レイアウトを持たないActivityで, アプリケーション起動時の処理などを行うものとする。)
② ログインがされていない場合は, LoginActivity に遷移し, その後ログインが完了したら HogeActivity に遷移
③ ログイン済みの場合は HogeActivity に直接遷移
④ HogeActivity においてログアウト処理が行われたら LoginActivity に遷移する。

ということを考える。
※ ちなみに, ログイン済みか否かは プリファレンスを利用して判断する。また, MainAvtivity はレイアウトを持たないので, android:nohistory 属性をtrueにして, バックキーで戻れないようにしておく。

この流れで作成したファイルは以下

< AndroidManifest.xml >

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.dev_grafr.app.logintest"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity"  
  8.                   android:label="@string/app_name"  
  9.                   android:noHistory="true"  
  10.                  android:launchMode="singleTask"  
  11.                   >  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.                 <category android:name="android.intent.category.LAUNCHER" />  
  15.             </intent-filter>  
  16.         </activity>  
  17.         <activity android:name=".LoginActivity"  
  18.                   android:label="@string/app_name"  
  19.                  android:launchMode="singleTask"  
  20.                   android:noHistory="true">  
  21.         </activity>  
  22.         <activity android:name=".HogeActivity"  
  23.                   android:label="@string/app_name">  
  24.         </activity>  
  25.     </application>  
  26. </manifest>  

< MainActivity.java >
  1. package com.dev_grafr.app.logintest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7.   
  8. public class MainActivity extends Activity {  
  9.     public static final String PREFERENCES_FILE_NAME = "preference";  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.   
  15.         if(loginCheck()){ // HogeActivity に遷移  
  16.             Intent intent = new Intent(getApplicationContext(), HogeActivity.class);  
  17.             startActivity(intent);  
  18.         }else// LoginActivity に遷移  
  19.             Intent intent = new Intent(getApplicationContext(), LoginActivity.class);  
  20.             startActivity(intent);  
  21.         }  
  22.     }  
  23.   
  24.     // ログイン判定  
  25.     public Boolean loginCheck(){  
  26.         SharedPreferences settings = getSharedPreferences(PREFERENCES_FILE_NAME, 0); // 0 -> MODE_PRIVATE  
  27.         if(settings == nullreturn false;  
  28.         int login = (int) settings.getLong("logged-in"0);  
  29.         if(login == 1return true;  
  30.         else return false;  
  31.     }  
  32. }  

< LoginActivity.java >

  1. package com.dev_grafr.app.logintest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. public class LoginActivity extends Activity {  
  11.     public static final String PREFERENCES_FILE_NAME = "preference";  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.login);  
  17.   
  18.         ((Button) findViewById(R.id.loginBtn)).setOnClickListener(new View.OnClickListener() {  
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 login();  
  22.                 // ログイン後は HogeActivity に遷移  
  23.                 Intent intent = new Intent(getApplicationContext(), HogeActivity.class);  
  24.                 startActivity(intent);  
  25.             }  
  26.         });  
  27.     }  
  28.      
  29.     // ログイン処理  
  30.     public void login(){  
  31.         SharedPreferences settings = getSharedPreferences(PREFERENCES_FILE_NAME, 0); // 0 -> MODE_PRIVATE  
  32.         SharedPreferences.Editor editor = settings.edit();  
  33.         editor.putLong("logged-in"1);  
  34.         editor.commit();  
  35.     }  
  36. }  

< HogeActivity.java >   ← 失敗例(正しいものは次回のブログで)

  1. package com.dev_grafr.app.logintest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. public class HogeActivity extends Activity {  
  11.     public static final String PREFERENCES_FILE_NAME = "preference";  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.hoge);  
  17.         ((Button) findViewById(R.id.logoutBtn)).setOnClickListener(new View.OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View v) {  
  20.                 logout();  
  21.                 Intent intent = new Intent(getApplicationContext(), LoginActivity.class);  
  22.                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // ← FLAG_ACTIVITY_CLEAR_TOPを設定  
  23.                 startActivity(intent);  
  24.             }  
  25.         });  
  26.     }  
  27.      
  28.     // ログアウト処理  
  29.     public void logout(){  
  30.         SharedPreferences settings = getSharedPreferences(PREFERENCES_FILE_NAME, 0); // 0 -> MODE_PRIVATE  
  31.         SharedPreferences.Editor editor = settings.edit();  
  32.         editor.putLong("logged-in"0);  
  33.         editor.commit();  
  34.     }  
  35. }  


[ 場合① ]
MainActivity -> LoginActivity -> HogeActivity と遷移した場合の Activityのスタックは, もちろん以下の通り
MainActivity は nohistory 属性が付いているので () をつけている
____________________________________
| ( MainActivity ) , LoginActivity , HogeActivity
____________________________________

そして, この時 HogeActivity において ログアウト処理を行い, LoginActivity に遷移する。その際にインテントに対して FLAG_ACTIVITY_CLEAR_TOP のフラグを付加することにより, HogeActivity が破棄されて遷移後のスタックは
____________________________________
| ( MainActivity ) , LoginActivity
____________________________________

となる。この場合は問題ない。

[ 場合② ]
しかし!!一度ログインを行い, ログイン済みの場合にアプリケーションを起動した際は, LoginActivity を介さないため,
____________________________________
| ( MainActivity ) , HogeActivity
____________________________________

というスタックになる。ここで ログアウト処理を行って LoginActivity に遷移した場合,
____________________________________
| ( MainActivity ) , HogeActivity , LoginActivity
____________________________________

となってしまうのである。つまり, ログアウトしたにも関わらず、LoginActivityにおいてバックキーを押すと, HogeActivityに戻ってしまう
そこで, HogeActivity でログアウト処理 ----> MainActivity ----> ログイン判定 ----> LoginActivity という遷移で,
FLAG_ACTIVITY_CLEAR_TOPフラグを付加したインテントにより MainActivity を経由すれば, MainActivity 以外がスタックから廃棄され上手くいくのではないか?と考えたのだが, MainActivity にはnohistory属性がついており, これがあるとうまくいかないという現象に陥った。

つまり, nohistory属性 がついていると, スタックに積まれないないため, FLAG_ACTIVITY_CLEAR_TOPを利用してもActivityが破棄されないのではないかという結論に至る(間違ってたらすいません)


実際には, ログインごはログイン画面にはバックキーでは戻れないようにするため, LoginActivityにもnohistory属性をつけるので, そうすると場合①もできないんですよね。笑
だって, MainActivity と LoginActivity が nohistory でスタックが
____________________________________
| HogeActivity
____________________________________

となると考えられるわけですから。。。


つまり, この方法だと上手なログアウト処理ができませんでした。

ログアウトした場合, LoginActivity を除いた全ての Activity をスタックから排除するにはどうすればいいのか・・・。
ということで、次回はこの解決法を説明します。
2011/03/15 0 コメント

Android - imeOptions 補足

キーボードの決定ボタン - imeOptions (EditText)の記事で, 決定ボタンのアイコンを変更したりしたのですが、Simejiなどのキーボードを使用した場合, アイコンが「改行」のままで, さらにそれをクリックした際のイベントが拾えてないことがわかりました。

< 対応前 (前回の記事の場合) >

  1. EditText edittext1 = (EditText) findViewById(R.id.edittext1);  
  2. edittext1.setOnEditorActionListener(new OnEditorActionListener() {  
  3.     @Override  
  4.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.         if (actionId == EditorInfo.IME_ACTION_SEARCH) {  
  6.             search(); // search処理  
  7.         }  
  8.         return true// falseを返すと, IMEがSearch→Doneへと切り替わる  
  9.     }  
  10. });  

< 対応後 (imeOptionsで指定できない場合にも対応) >

  1. EditText edittext1 = (EditText) findViewById(R.id.edittext1);  
  2. edittext1.setOnEditorActionListener(new OnEditorActionListener() {  
  3.     @Override  
  4.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.         if (actionId == EditorInfo.IME_ACTION_SEARCH || event.getAction() == KeyEvent.ACTION_UP) {  
  6.             search(); // search処理  
  7.         }  
  8.         return true// falseを返すと, IMEがSearch→Doneへと切り替わる  
  9.     }  
  10. });  

これで一応解決できました。(ほかに解決法ないのかな。。)
2011/03/15 0 コメント

Android - スレッドとActivity操作メモ

画像を読み込む際に, 他リソースにある画像をURLで指定し, 取得&描写ということをやりたかったのですが、普通に(スレッドを考えず)やってしまうと、画像取得の際の処理時間がボトルネックになってしまい。全体として描写時間がかかってしまうということに・・・

そこで、スレッドを利用して取得&描写処理を行おうとしたのですが、どうもエラーが・・
ということで、解決したのでメモ。

原因 :「メインスレッドで直接Activityの操作をすることはできない」
解決法:「runOnUiThreadを使用する!(UI用のスレッドから操作すればおk)」
2011/03/14 2 コメント

Android - キーボードの決定ボタン - imeOptions (EditText)

東北地方太平洋沖地震ということで、とても大変な状況ですが、節電しつつ自分なりに普段の生活を送ろうとAndroidアプリ開発を再開 。


EditTextで, IMEの決定アクションを行っ際のイベントを取得したかった為,それに関する内容をまとめました。


まず, EditTextの android:imeOptions フィールドに, actionNone, actionDone, actionNext, actionSend, actionGo, actionSearch
などの値を設定することができます。

これらの値を設定した場合, アイコンが以下の様になります。

< actionNone >


< actionDone >


< actionNext >


< actionSend >


< actionGo >


< actionSearch >



で、これを設定した際のアクションを設定する場合は, 以下のようにすれば良いらしいです。

  1. EditText edittext1 = (EditText) findViewById(R.id.edittext1);  
  2. edittext1.setOnEditorActionListener(new OnEditorActionListener() {  
  3.     @Override  
  4.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.         if (actionId == EditorInfo.IME_ACTION_SEARCH) {  
  6.             search(); // search処理  
  7.         }  
  8.         return true// falseを返すと, IMEがSearch→Doneへと切り替わる  
  9.     }  
  10. });  

  1. <EditText  
  2.     android:id="@+id/edittext1"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:inputType="text"  
  6.     android:imeOptions="actionSearch"  
  7.     android:hint="てすと"  
  8.     />  


ご参考までに。
2011/03/08 0 コメント

MacBookPro (MC374J/A) メモリ交換

去年(2010年)の5月頃に購入したMacBookProのメモリを増設しました!
MacBookPro の型番(?)は MC374J/A。

2G×2 から 4G×2へメモリを増設です。いちおうそのときの手順を。
まず購入したメモリは, Buffaloのメモリ

夜中11時に注文して, 翌日の6時頃届きました↓ってことで早速作業作業!!



まず, 裏っ返してネジを外します!


そして蓋をあけます!(こんな感じ↓)


で, メモリを外していきます。




で, 2枚とも外したら, 新しく4Gのメモリに入れ替えます!これでOK!
PCのほうで8Gとメモリを認識できていました!!

8Gにすると、VM使ってwindows動かした時でも、サクサクです!!
2011/03/07 0 コメント

Android - カスタムダイアログの作成方法

カスタムダイアログの作成方法について。例えばこんな感じ↓



デフォルトで用意されている種類のダイアログではなくて, カスタマイズして独自のダイアログの作成を行いたい場合, setView() をしてあげれば良いだけです。


  1. LayoutInflater factory = LayoutInflater.from(MyProductsActivity.this);  
  2. // custom_dialog.xmlに, レイアウトを記述  
  3. final View inputView = factory.inflate(R.layout.custom_dialog, null);  
  4.   
  5. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  6.     builder.setTitle("選択してください"); // タイトル設定  
  7.                .setView(inputView) // レイアウト設定  
  8.                .setNegativeButton("閉じる"new DialogInterface.OnClickListener() { // 閉じる際のイベント  
  9. })  
  10.                .create().show(); // 表示  


まぁこんな感じです。つまり, setView()するだけです。
2011/03/05 2 コメント

Android - (ListView) 区切りフィールド付き+インデクス表示付きのスクロール

ListViewで区切りフィールド付き+インデクス表示付きのスクロールをしたい!

なにが言いたいかというと, 下のように,

①「セクションの区切りの部分のフィールド」
②「右の部分の高速スクロール用のつまみ」
③「中央に表示されているインデックス文字の表示」

を行う方法について, ちょっと解説していきます。(諸事情により, 画像の内容加工して文字を先頭以外見えなくしています。笑)



これは調べてもなかなか情報がなかったため、いっそのことまとめておけば、こんな自分でも役立つかなー(?)と。笑
方針としては,

①「セクションの区切りの部分のフィールド」 => isEnabledのオーバーライドを使用して, 区切りの場合はViewを変更する
②「右の部分の高速スクロール用のつまみ」=> ListViewに対して android:fastScrollEnabled="true" 設定
③「中央に表示されているインデックス文字の表示」=> SectionIndexerをimplementsする

必要な部分のみ記述してます。

<list.xml> メインのレイアウトファイル
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     >  
  7.       
  8.     <ListView  
  9.         android:id="@+id/ListViewSample"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="0dip"  
  12.         android:layout_weight="1"  
  13.         android:fastScrollEnabled="true" <!-- スクロール用のつまみ表示 -->  
  14.         >  
  15.     </ListView>  
  16.       
  17. </LinearLayout>  


<listview_item.xml> リストの各項目のためのレイアウトファイル
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     >  
  7.     <TextView        android:id="@+id/listview_name"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         />  
  11. </LinearLayout>  


<listview_sep.xml> 区切り部分のレイアウトファイル
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     android:background="#AAA"  
  7.     android:paddingLeft="10px"  
  8.     >  
  9.     <TextView  
  10.         android:textColor="#FFFFFF"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:id="@+id/listView_initial"  
  14.         >  
  15.     </TextView>  
  16. </LinearLayout>  


<CustomAdapter.java>
  1. public class CustomAdapter extends ArrayAdapter<Contentvalues> implements SectionIndexer{  
  2.     private LayoutInflater layoutInflater_;  
  3.     private int separetorResourceId = R.layout.listview_sep; // 区切り用のフィールド  
  4.     private final String SEP_FLAG = "sepFlag";  
  5.     private HashMap<String, Integer> indexer = new HashMap<String, Integer>();  
  6.     private String[] sections;  
  7.       
  8.     // Constructor  
  9.     public CustomAdapter(Context context, int textViewResourceId, List<Contentvalues> objects) {  
  10.          // 一度空のobjectで初期化 (addで区切りを入れながら追加するため)  
  11.         super(context, textViewResourceId, new ArrayList<contentvalues>());  
  12.         layoutInflater_ = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  13.           
  14.         /// 区切りを挿入しながらデータを追加  
  15.         int listLength = objects.size();  
  16.             String pre_initial = ""int sep_num = 0;  
  17.             for(int index=0; index<listLength; index++){  
  18.                 ContentValues cv = objects.get(index);  
  19.   
  20.                 String initial = cv.getAsString("name").substring(01); // nameの頭文字を基準に区切る  
  21.                 if(!initial.equalsIgnoreCase(pre_initial)){ // 頭文字の判定(頭文字が変わったら追加)  
  22.                     ContentValues cv_sep = new ContentValues();  
  23.                     cv_sep.put(SEP_FLAG, true); cv_sep.put("text", initial);  
  24.                     this.indexer.put(initial, index + sep_num);  
  25.                     add(cv_sep); sep_num++;  
  26.                     pre_initial = initial;  
  27.                 }  
  28.             add(cv); // ArrayAdapterにObjectを追加  
  29.           }  
  30.           
  31.         ArrayList<string> sectionList = new ArrayList<String>(indexer.keySet());   
  32.         Collections.sort(sectionList);  
  33.         sections = new String[sectionList.size()];  
  34.         sectionList.toArray(sections);  
  35.     }  
  36.       
  37.     // ViewHolderクラス  
  38.     static class ViewHolder{  
  39.         TextView textView_name;  
  40.     }  
  41.       
  42.     @Override  
  43.     public View getView(int position, View convertView, ViewGroup parent){  
  44.           
  45.         // 特定の行(position)のデータを得る  
  46.         final ContentValues cv = (ContentValues)getItem(position);  
  47.         ViewHolder holder;  
  48.           
  49.         // convertViewは使い回しされている可能性があるのでnullの時と, 区切り用のビューに設定されている場合に新しく作成  
  50.         if (null == convertView || convertView.getId() != R.layout.listview) {  
  51.             convertView = layoutInflater_.inflate(R.layout.listview, null);  
  52.             holder = new ViewHolder();  
  53.             holder.textView_name = (TextView)convertView.findViewById(R.id.listview_name);  
  54.             convertView.setTag(holder);  
  55.         }else{  
  56.             holder = (ViewHolder) convertView.getTag();  
  57.         }  
  58.           
  59.         if(!isEnabled(position)){ // 区切りの場合は, 区切り用のビューを変更する  
  60.             convertView =  layoutInflater_.inflate(separetorResourceId, null);  
  61.             TextView sep_text = (TextView)convertView.findViewById(R.id.listView_initial);  
  62.             sep_text.setText(cv.getAsString("text")); // 区切りに表示するテキストを設定  
  63.         }else// 区切りでない場合   
  64.             holder.textView_name.setText(cv.getAsString("name"));  
  65.         }  
  66.           
  67.         return convertView;  
  68.     }  
  69.   
  70.     @Override  
  71.     public boolean isEnabled(int position){ // 区切りの場合はfalse, そうでない場合はtrueを返す  
  72.         ContentValues cv = getItem(position);  
  73.         if(cv.containsKey(SEP_FLAG)){ // 区切りフラグの有無チェック  
  74.             return !cv.getAsBoolean(SEP_FLAG);  
  75.         }else{  
  76.             return true;  
  77.         }  
  78.     }  
  79.       
  80.     // 以下は, SectionIndexerの実装部分  
  81.       
  82.     @Override  
  83.     public int getPositionForSection(int section) {  
  84.         return indexer.get(sections[section]);  
  85.     }  
  86.   
  87.     @Override  
  88.     public int getSectionForPosition(int position) {  
  89.         return 1;  
  90.     }  
  91.   
  92.     @Override  
  93.     public Object[] getSections() {  
  94.         return sections;  
  95.     }  
  96.   
  97. }  


ブログに載せるために, 本来のソースではなく, 余分な部分をカットしながら記事を書いていて、今回記述したソース自体は実行していないので、もしかしたら細かいエラーがあるかも。その場合はご連絡してもらえるとありがたいです。でもまぁ、方針的にはこれでできるので許してください。。。

また、ArrayAdapter<Contentvalues> の型で行っていますが、別にContentvaluesじゃなくて専用のクラスをつくっちゃってもいいですね(そっちの方が分かりやすくていいかも。笑)そして、メモ書きという程度で書いていて、今回頭文字の判定の部分はテキトーです(英字のUpperCaseとLowerCaseの判定や, 「カ」と「ガ」を同じセクションにしたり・・・などという細かいことはしてません)。すいません。。

一度案件がおちついて整理する時間があったら書き直そうかと思ってます。
2011/03/03 0 コメント

Android - 固定ヘッダー付きのListView - その2

固定ヘッダー付きのListView - その1
では、RelativeLayoutを使用したけど、LinearLayoutだけで、もっと簡単にできたので(なんで気づかなかったんだろうw)、メモメモ。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="horizontal"  
  12.         >  
  13.   
  14.         [ ヘッダー内容 ]  
  15.   
  16.     </LinearLayout>  
  17.   
  18.     <ListView  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="0dip"  
  21.         android:layout_weight="1"  
  22.         android:id="@+id/ListViewSample"  
  23.         >  
  24.     </ListView>  
  25.   
  26.     <LinearLayout  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:orientation="horizontal"  
  30.         >  
  31.   
  32.         [ フッター内容 ]  
  33.   
  34.     </LinearLayout>  
  35. </LinearLayout>  


ListViewの部分に
android:layout_height="0dip"
android:layout_weight="1"

を指定してあげるのがミソ。
簡単ですね〜。
2011/03/01 0 コメント

iphoneの開発環境を整える

Androidだけでなく、iPhoneアプリもやろうと開発環境を構築 (これは仕事でなく趣味で)

OS : Mac OS X 10.6

Apple Developer にアクセスし, iOS Dev Centerページへ
② アカウントを持っている場合は「Log In」を、持っていない場合は「Register」からアカウント登録を行ってください(※アカウント登録の際は, すべて英語で記述してください。日本語だと文字化けしたりします。)
③ 登録が終わったら, Xcode and iOS SDK をダウンロード & インストール
④ SDKには, 開発に使用するXcodeも含まれています。(/Developer/Applications/Xcode.app)

こおれでOK。
ちなみにSVNを使用する場合は,

① svnリポジトリ用ディレクトリの作成
② /usr/bin/svnadmin create 作成したディレクトリのフルパス (これでリポジトリ作成)
③ メニュー -> SCM -> SCMリポジトリを構成 から設定を行う (Xcodeにリポジトリを追加する場合は左下の「+」を押せばできます)
2011/02/28 0 コメント

Android - メニューの表示

メニューを作成した場合はいかのようにすればOK


こんな感じです


  1. // メニューの設定  
  2. @Override  
  3. public boolean onCreateOptionsMenu(Menu menu) {  
  4.     super.onCreateOptionsMenu(menu);  
  5.     menu.add(0 , Menu.FIRST , Menu.NONE , "MENU1").setIcon(アイコン);  
  6.     menu.add(0 , Menu.FIRST + 1 , Menu.NONE , "MENU2").setIcon(アイコン);  
  7.     menu.add(0 , Menu.FIRST + 2 , Menu.NONE , "MENU3").setIcon(アイコン);  
  8.     return true;  
  9. }  
  10.   
  11. // メニューが押された場合  
  12. @Override  
  13. public boolean onOptionsItemSelected(MenuItem item) {  
  14.     boolean ret = true;  
  15.     Intent intent = new Intent();  
  16.     switch (item.getItemId()) {  
  17.         case Menu.FIRST:  
  18.             // 項目IDが Menu.FIRST のものを押された際の処理  
  19.             break;  
  20.         case Menu.FIRST + 1:  
  21.             // 項目IDが Menu.FIRST + 1 のものを押された際の処理  
  22.             break;  
  23.         case Menu.FIRST + 2:  
  24.             // 項目IDが Menu.FIRST + 2 のものを押された際の処理  
  25.             break;  
  26.         }  
  27.     return ret;  
  28. }  


ちなみに, menu.add(int groupId, int ItemId, int order, CharSequence title); の順番
Menu.NONE (= 0)をorderに指定した場合は, addをした順番でメニューに表示されます。

また, Androidで表示できるメニュー数は6つまでとなっていて、ここで7つ以上のメニューを設定した場合は, 1~5番目の項目に加えて「MORE」という項目が加わったメニューが表示され、MOREをタップすると, 残りの6番目以降の項目が表示されると言った形になります。

2011/02/27 0 コメント

Android - 固定ヘッダー付きのListView - その1

固定ヘッダー付きのListViewを使用する場合 (Webページでいう position:fixed; のヘッダー)

ヘッダーに 「Left Button」「Center Button」「Right Button」を配置した場合は以下のようにすればOK

こんな感じになります。(スクロールしても、ヘッダーの部分は固定です。)


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:layout_width="wrap_content"  
  4.  android:layout_height="wrap_content"  
  5.  >  
  6.  <LinearLayout  
  7.   android:id="@+id/header_menu"  
  8.   android:layout_width="fill_parent"  
  9.   android:layout_height="wrap_content"  
  10.      android:orientation="horizontal"  
  11.   >  
  12.   <Button  
  13.    android:layout_width="wrap_content"  
  14.    android:layout_height="wrap_content"  
  15.    android:text="Left Button"  
  16.    />  
  17.   <Button  
  18.    android:layout_width="wrap_content"  
  19.    android:layout_height="wrap_content"  
  20.    android:text="Center Button"  
  21.    android:layout_weight="1"  
  22.    />  
  23.   <Button  
  24.    android:layout_width="wrap_content"  
  25.    android:layout_height="wrap_content"  
  26.    android:text="Right Button"  
  27.    />  
  28.  </LinearLayout>  
  29.  <ListView  
  30.   android:layout_width="fill_parent"  
  31.   android:layout_height="wrap_content"  
  32.   android:id="@+id/ListViewSample"  
  33.   android:layout_below="@id/header_menu"  
  34.   >  
  35.  </ListView>  
  36. </RelativeLayout>  

フッターもつけたい場合は,
http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/
を参考にするとよし。
2011/02/27 0 コメント

MacローカルでSVN - subversive (eclipse) 導入

eclipseにsubversiveプラグインを入れて, SVN環境を整えました。
そのときの導入方法まとめ

① eclipseメニューから, 「ヘルプ -> 新規ソフトウェアのインストール」
② http://download.eclipse.org/technology/subversive/0.7/update-site/ を指定
③ Subversive 統合プラグイン (インキュベーション) と Subversive SVN チーム・プロバイダー・プラグイン (インキュベーション) を選択 (Subversive ソース (インキュベーション)は今回導入していません)
④ 上記のプラグインをインストール

これで, Eclipseへのインストールは完了.
次に, Macローカルにリポジトリを作成します。リポジトリは, /Users/ユーザー名/svn に作成する事とします。

① まずリポジトリとなるsvnディレクトリを作成 (mkdir /Users/ユーザー名/svn)
② /usr/bin/svnadmin create /Users/ユーザー名/svn を実行し, リポジトリを作成
③ このパスをEclipseで設定 (プロジェクト名の上, 等で右クリックメニュー -> チーム -> プロジェクトの共有)
④ リポジトリのパスに, file:///Users/ユーザー名/svn を指定

これで完了。subversiveが使えます。
2011/02/26 0 コメント

Android - ListViewにTextViewとCheckboxを配置

< 問題点 >
ListView上にTextViewを配置した場合, その項目上をタップすると, ListViewのクリックイベントが発生する。
これは良いのだが、TextViewだけでなく, ListViewにTextViewと「Checkbox」を配置した場合、テキストの部分(TextView)をタップしても反応しなくなってしまう。(チェックボックスの方にフォーカスが移ってしまっていて, チェックボックスしか反応しなくなる、)

<解決法>
そこで, チェックボックスに対してのタップイベントもListViewに対するイベントも両方独立して使用可能にするには, Checkboxに対して

android:focusable="false"
android:focusableInTouchMode="true"

を設定してあげれば良いらしい。
2011/02/25 0 コメント

Android - ArrayAdapterメモ

ArrayAdapterは, 指定した1つの TextView に対してテキストをセットするためのAdapterとのこと。

http://d.hatena.ne.jp/androidprogram/20100703/1278112044 のサイトがとても分かりやすく解説してくれていた。


つまり, 複数のTextViewに対しては操作できない!ということである。
もし複数のTextView等に対して操作したい場合は, ArrayAdapterを拡張する必要があるっぽい。

拡張したい場合は以下のサイトを参考にさせていただいた。
http://isakado.blog106.fc2.com/category0-1.html
2011/02/20 0 コメント

Android - Error generating final archive: Debug certificate expired on ...

突然 Error generating final archive: Debug certificate expired on ...
というエラーでデバッグできなくなってしまった。これはkeystoreの有効期限が問題らしく

http://magpad.jugem.jp/?eid=151

を参考にさせていただき、解決できました。
2011/01/18 0 コメント

さくらインターネットでsymfony1.4(.8) - プロジェクトの作成

「sample」プロジェクトを作成する。
自分のサーバーにインストールされているsymfonyは ver. 1.4.8 です。

とりあえず, ~/symfony ディレクトリ(無い場合は作成)内に, sampleディレクトリを作成&移動。
% mkdir -p ~/symfony/sample
% cd symfony/sample

次にプロジェクトを作成します。1.4以上は以下のようにプロジェクトを作成する。
(sampleディレクトリで実行)
% symfony generate:project sample
そして, アプリケーションの作成
% symfony generate:app frontend



とりあえず, これでプロジェクトの作成ができます。1.4以前は違うコマンドのようなので注意!
2011/01/18 0 コメント

さくらインターネットでsymfony1.4

さくらインターネットのレンタルサーバーで, symfony1.4をインストールしてみました。そのときのメモ。

参考にさせていただいた、サイトはこちらです↓↓
ueblog [ http://ueblog.natural-wave.com/2008/06/15/sakura-symfony/ ]
さぼてん [ http://saboten009.blogspot.com/2009/02/symfony12_07.html ]

% mkdir local
% cd local

まずは, PEARのインストールを行います。

% curl http://pear.php.net/go-pear > go-pear.php
% php go-pear.php

ENTERを押していき,
....
1. Installation prefix ($prefix) : /home/[account名]/local
2. Temporary files directory : $prefix/temp
3. Binaries directory : $prefix/bin
4. PHP code directory ($php_dir) : $prefix/PEAR
5. Documentation base directory : $php_dir/docs
6. Data base directory : $php_dir/data
7. Tests base directory : $php_dir/tests


1-7, 'all' or Enter to continue:

のように、1が/home/[account名]/localとなっていることを確認して, ENTER!

Would you like to install these as well? [Y/n] : → Y
Would you like to alter php.ini ? [Y/n] : → n (php.iniの書き換えはできないため)

% set path = (/sbin /bin $HOME/local/bin $HOME/bin /usr/local/bin/)


% pear -V
を行い, PHPのバージョンが5であることを確認する。

これで, PEARのインストール終了!
次に, symofonyのインストールを行います。


% pear channel-discover pear.symfony-project.com
% pear install symfony/symfony

最後に
% symfony -V

で, symfonyのバージョンを確認して下さい。
symfony へのPATHが通っていない場合は,

% set path = (/sbin /bin $HOME/local/bin $HOME/bin /usr/local/bin/)

を再度実行してみてください。


これで、symfonyのインストールは終了です!
 
;