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にリポジトリを追加する場合は左下の「+」を押せばできます)
 
;