で
protected Dialog onCreateDialog(int id) {
String[] projection = new String[] {
Phone._ID,
Phone.DISPLAY_NAME,
Phone.NUMBER
};
Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, null, null, null);
return new AlertDialog.Builder(ContactActivity.this)
.setTitle("Select Contacts")
.setCursor(cursor,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
/* User clicked on a contact item */
Toast.makeText(getApplicationContext(),
"CLICKED-"+which,
Toast.LENGTH_SHORT).show();
}
}, ContactsContract.Contacts.DISPLAY_NAME)
.create();
}
おかげで私は自分自身、このためのソリューションを見つけました。以下は私が今使っているコードです。
連絡先リストのクエリのカーソルを最終として宣言しました。 とするのonClickメソッド内同じカーソル・オブジェクトを使用するカーソルのmoveToPosition()
方法 を使用して、クリックした位置にレコードを取得します。 onClickの の下にcursor.moveToFirst();
を使用する必要があります。それ以外の場合は、 ダイアログを再度呼び出すと、ダイアログが空白になります(なぜそうなるのかわかりません)。
protected Dialog onCreateDialog(int id) {
String[] projection = new String[] {
Phone._ID,
Phone.DISPLAY_NAME,
Phone.NUMBER
};
final Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, null, null, null);
return new AlertDialog.Builder(ContactActivity.this)
.setTitle("Select Contacts")
.setCursor(cursor,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
/* User clicked on a contact item */
if(cursor.moveToPosition(which)){
long contactId=cursor.getLong(0);
String name=cursor.getString(1);
String number=cursor.getString(2);
Toast.makeText(getApplicationContext(),
"You selected: ["+contactId+"]"
+ name + " , " +number,
Toast.LENGTH_SHORT)
.show();
}
cursor.moveToFirst();//Reset the position
}
}, ContactsContract.Contacts.DISPLAY_NAME)
.create();
}
しかし、私はそれが正しい方法であるとは思わない。 私はこの質問の答えとしてこれをマークしていません。 いつでもこの質問を残しておきます。 誰かがより良い提案を出すことができます。
UPDATE:が無応答ための答えとしてマーク。