2011年10月4日火曜日

Android AsyncQueryHandler を使う

UI スレッド上で getContentResolver().query() とかをすると、処理が重たい場合にスレッドをブロックしてしまって ANR になることもあるので、別スレッドで非同期に query() するための AsyncQueryHandler というクラスがあります。

AsyncQueryHandler は abstract クラスなので、継承したクラスを作って、onQueryComplete()onQueryInsertComplete() を Override し、ここで Adapter に Cursor をセットします。
こんな感じで使います。

  1. public class AsyncQueryHandlerSampleActivity extends ListActivity {  
  2.   
  3.     private static final String[] PHONE_PROJECTION = new String[] {  
  4.         Phone._ID,  
  5.         Phone.TYPE,  
  6.         Phone.NUMBER  
  7.     };  
  8.   
  9.     private static final int COLUMN_TYPE = 1;  
  10.   
  11.     private static final int[] COLOR_LIST = new int[] {  
  12.         Color.parseColor("#002A42"),  
  13.         Color.parseColor("#3DC3EA"),  
  14.         Color.parseColor("#99417B"),  
  15.         Color.parseColor("#F2AE30"),  
  16.         Color.parseColor("#F2D338"),  
  17.     };  
  18.       
  19.     public class CustomAsyncQueryHandler extends AsyncQueryHandler {  
  20.   
  21.         public CustomAsyncQueryHandler(ContentResolver cr) {  
  22.             super(cr);  
  23.         }  
  24.           
  25.         @Override  
  26.         protected void onQueryComplete(int token, Object cookie, Cursor cursor) {  
  27.             super.onQueryComplete(token, cookie, cursor);  
  28.               
  29.             SimpleCursorAdapter adapter = new SimpleCursorAdapter(  
  30.                     AsyncQueryHandlerSampleActivity.this,  
  31.                     android.R.layout.simple_list_item_2,   
  32.                     cursor,  
  33.                     new String[] { Phone.TYPE, Phone.NUMBER },  
  34.                     new int[] { android.R.id.text1, android.R.id.text2 });  
  35.               
  36.             adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {  
  37.                   
  38.                 public boolean setViewValue(View view, Cursor cursor, int columnIndex) {  
  39.                     if (columnIndex == COLUMN_TYPE) {  
  40.                         int type = cursor.getInt(COLUMN_TYPE);  
  41.                         String text = (String) Phone.getTypeLabel(getResources(), type, null);  
  42.                           
  43.                         ((TextView) view).setText(text);  
  44.                         ((TextView) view).setTextColor(COLOR_LIST[type % 5]);  
  45.                         return true;  
  46.                     }  
  47.                     return false;  
  48.                 }  
  49.             });  
  50.             setListAdapter(adapter);  
  51.         }  
  52.           
  53.     }  
  54.       
  55.     @Override  
  56.     protected void onCreate(Bundle savedInstanceState) {  
  57.         super.onCreate(savedInstanceState);  
  58.           
  59.         CustomAsyncQueryHandler asyncQueryHandler = new CustomAsyncQueryHandler(getContentResolver());  
  60.         asyncQueryHandler.startQuery(  
  61.                 0// token  
  62.                 null// cookie  
  63.                 Phone.CONTENT_URI,  
  64.                 PHONE_PROJECTION,   // projection  
  65.                 null,   // selection  
  66.                 null,   // selectionArgs  
  67.                 null    // orderBy  
  68.                 );  
  69.     }  
  70. }  


0 件のコメント:

コメントを投稿