我正在尝试从一组已知 id 的特定组中获取联系人。我能够获取该组中的组 id 和联系人姓名。但我无法获取联系人号码。尝试了一些来自谷歌搜索的解决方案,但每次我得到的电话号码与组 id 相同,如果我使用 Phone.number、Phone.DISPLAY_NAME 查询组的电话号码和联系人姓名。如果我使用以下方法我收到错误请帮助我找出我的错误代码。
代码 :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_contacts);
addGroups = (Button) findViewById(R.id.selectGroup);
groupAdapter = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.select_dialog_singlechoice, GroupCursor(),
new String[] { ContactsContract.Groups.TITLE },
new int[] { android.R.id.text1 });
setListAdapter(groupAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
addGroups.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(myGroups.this, Main.class);
Bundle carry = new Bundle();
carry.putStringArrayList("numbers", cNumbers);
carry.putStringArrayList("name", cNames);
intent.putExtras(carry);
setResult(RESULT_OK, intent);
finish();
}
});
}
private Cursor GroupCursor() {
String[] projection = { ContactsContract.Groups.TITLE,
ContactsContract.Groups._ID };
Cursor gCursor = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, projection, null, null,
ContactsContract.Groups.TITLE);
// int idcolumn = gCursor.getColumnIndex(ContactsContract.Groups._ID);
// String id = gCursor.getString(idcolumn);
// Log.d(E, "group id : " + id );
return gCursor;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d(E, "id : " + id);
String groupId = Long.toString(id);
String[] cProjection = { Phone.NUMBER, Phone.DISPLAY_NAME ,Contacts.DISPLAY_NAME, Contacts._ID};
Cursor groupCursor = getContentResolver().query(Data.CONTENT_URI,
cProjection,
CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?",
new String[]{groupId}, null);
if (groupCursor != null) {
groupCursor.moveToFirst();
do {
int nameCoumnIndex = groupCursor
.getColumnIndex(Phone.DISPLAY_NAME);
String name = groupCursor.getString(nameCoumnIndex);
String cId = groupCursor.getString(groupCursor.getColumnIndex(Contacts._ID));
Cursor numberCursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID +"="+cId, null, null);
numberCursor.moveToFirst();
int numberColumnIndex = numberCursor
.getColumnIndex(Phone.NUMBER);
Log.d(E, "numberindex : " + numberColumnIndex);
String number = numberCursor.getString(numberColumnIndex);
cNumbers.add(number);
Log.d(E, "contact " + name + ":" + number);
cNames.add(name);
} while (groupCursor.moveToNext());
}
}
}
日志 :
09-04 17:59:24.943: D/dalvikvm(18084): GC_EXTERNAL_ALLOC freed 23K, 48% free 2844K/5379K, external 1032K/1039K, paused 24ms
09-04 17:59:26.054: D/(18084): id : 14
09-04 17:59:26.074: D/(18084): numberindex : 0
09-04 17:59:26.074: D/AndroidRuntime(18084): Shutting down VM
09-04 17:59:26.074: W/dalvikvm(18084): threadid=1: thread exiting with uncaught exception (group=0x40015568)
09-04 17:59:26.084: E/AndroidRuntime(18084): FATAL EXCEPTION: main
09-04 17:59:26.084: E/AndroidRuntime(18084): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
09-04 17:59:26.084: E/AndroidRuntime(18084): at hm.swarna.groupsms.myGroups.onListItemClick(myGroups.java:117)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.widget.ListView.performItemClick(ListView.java:3535)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1827)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.os.Handler.handleCallback(Handler.java:587)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.os.Looper.loop(Looper.java:130)
09-04 17:59:26.084: E/AndroidRuntime(18084): at android.app.ActivityThread.main(ActivityThread.java:3703)
09-04 17:59:26.084: E/AndroidRuntime(18084): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 17:59:26.084: E/AndroidRuntime(18084): at java.lang.reflect.Method.invoke(Method.java:507)
09-04 17:59:26.084: E/AndroidRuntime(18084): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-04 17:59:26.084: E/AndroidRuntime(18084): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-04 17:59:26.084: E/AndroidRuntime(18084): at dalvik.system.NativeStart.main(Native Method)
09-04 17:59:27.556: I/Process(18084): Sending signal. PID: 18084 SIG: 9