ゆえにそんなにリファレンスサイトがないので自分でどーこーしてみることにした。
某会議でよく電話帳が話題になるので
まー、興味がでたっていえばでたんですけど。
Androidでは電話帳DBなるものが標準で搭載されている。
/data/data/com.android.providers.contacts/databases/contacts2.db
という場所にあったりする。
実際にデータをpullなどでぬいてきて
SQLite3のビューアーとかでみるとたしかにデータがはいっている。
が、これがすごくわかりづらい。
一回に解説したいですが、私にもわかってない箇所があるので
まー、それはゆっくりと。
とりあえず2.1からでは「ごっそりと仕組みが変わった」ということを覚えていただきたいです。
覚えなくてもいいけど。
では、簡単につかってみましょう。
前提条件として電話帳にこんな感じのデータをいれておきます。
これは標準電話帳の画面です。
で、最終的にこんな感じにする。
今回はかるーくテストなんで名前だけの表示にしています。
本来なら、下に電話番号をだすなり、メアドだすなり好きにしていいんですが
仕事中の休み時間にできることは限られているので(1時間しかない)。
時間があればそのうちハイパーなものをつくりたんですが、はてさて。
では本日のコードです。
まずはレイアウトのXMLを。
- <!--xml version="1.0" encoding="utf-8"?-->
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
- <listview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listView"></listview>
- </linearlayout>
簡単にListViewだけを表示しているのでこれだけです。
つづいてはこちらに名前を入れるコード。
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class AddressListActivity extends Activity {
- private ListView mListView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.address_list);
- // 表示
- mListView = (ListView) findViewById(R.id.listView);
- mListView.setAdapter(displayNameList());
- }
- // DBから取り出してごにょっとする
- private ArrayAdapter<string> displayNameList() {
- // DBアクセス
- databaseAccess access = new databaseAccess(this);
- Cursor cursor = access.selectAllAddress();
- cursor.moveToFirst();
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1);
- // Contactsのデータを管理しているRAW_CONACT_ID
- int idIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.RAW_CONTACT_ID);
- // 表示名
- int nameIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME);
- // idにとりあえずの初期値を入れておく
- int id = 0;
- while (!cursor.isAfterLast()) {
- // idが変わったら次の人
- if(id != cursor.getInt(idIndex)) {
- // idの入れ替え
- id = cursor.getInt(idIndex);
- // 表示名取得
- adapter.add(cursor.getString(nameIndex));
- }
- cursor.moveToNext();
- }
- cursor.close();
- return adapter;
- }
- }
- </string></string></string>
コメントいれてあるのですが、実は上のコードにはDBにアクセスする処理はございません。
アクセスするにはこちらのコードを。
別にクラスを分ける必要性はなかったのですが、
あとあと入力とかを拡張しようと思っていたのでわけました。
- import android.content.Context;
- import android.database.Cursor;
- import android.net.Uri;
- import android.provider.ContactsContract;
- public class databaseAccess {
- private Context context;
- private Uri uri = ContactsContract.Data.CONTENT_URI;
- // コンストラクタ
- public databaseAccess(Context context){
- this.context = context;
- }
- /**
- * DB全件返しメソッド
- * @return
- */
- public Cursor selectAllAddress() {
- return context.getContentResolver().query(uri, null, null, null, null);
- }
- }
クエリのつかいかたですが、
- Cursor android.content.ContentResolver.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
という記述がDevelopersなんとかっていう解説書(もう忘れた)にあります。
場所を指定して、取り出すものを指定。さらに条件・・・といったところでしょうか。
今回は簡易なのでいやでも全件返すようにしています。
全件返すことでもちろん名前以外にも電話番号まで帰ってきているので
- while (!cursor.isAfterLast()) {
- // idが変わったら次の人
- if(id != cursor.getInt(idIndex)) {
- // idの入れ替え
- id = cursor.getInt(idIndex);
- // 表示名取得
- adapter.add(cursor.getString(nameIndex));
- }
- cursor.moveToNext();
- }
このあたりのところでいろいろ分岐させています。
IDは一人につき1つなので、そのIDが切り替わったら次の人。
同じIDが名前を2回もっていることもあるのでこういう重複チェックをかけています。
なぜ名前を2回もっているのかというと・・・
「ID」「表示名」「電話番号」
「ID」「表示名」「苗字」
というようなデータ構造になっているためです。
表示名は何回もはいってくるのですね(汗)
なのでまわりくどいコードになっています。
このあたりは私の調査不足なのでおいおいかっこいいコードに(そりゃもう鼻血がでるほど)していけたらいいなーとおもいますがな。
そんな感じで第一回電話帳会議終了。
ではもどるぞ!!だいじゅうよんてい(ry)
はい、仕事します。
0 件のコメント:
コメントを投稿