关于同一主题有很多讨论,但是在这里花了4个小时后,我找不到有效的描述或链接来与Checkbox建立联系人选择器。
我的活动有完成按钮,listview
有checkbox
。我已设法正确显示联系人。现在,我想以bundle
(我认为最好的方式)返回选定的联系电话号码,以便我可以在onActivityResult()
中获取号码列表。我不确定我追随的方式是否正确。
这是我的代码:
public class ContactPickerMulti extends ListActivity implements OnClickListener {
// List variables
public String[] Contacts = {};
public int[] to = {};
public ListView myListView;
Button save_button;
private TextView phone;
private String phoneNumber;
private Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_multi);
// Initializing the buttons according to their ID
save_button = (Button) findViewById(R.id.contact_done);
// Defines listeners for the buttons
save_button.setOnClickListener(this);
Cursor mCursor = getContacts();
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_multiple_choice,
mCursor,
Contacts = new String[] { ContactsContract.Contacts.DISPLAY_NAME },
to = new int[] { android.R.id.text1 });
setListAdapter(adapter);
myListView = getListView();
myListView.setItemsCanFocus(false);
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
public void onClick(View src) {
Intent i;
switch (src.getId()) {
case R.id.contact_done:
SparseBooleanArray selectedPositions = myListView
.getCheckedItemPositions();
SparseBooleanArray checkedPositions = myListView
.getCheckedItemPositions();
if (checkedPositions != null) {
for (int k = 0; k < checkedPositions.size(); k++) {
if (checkedPositions.valueAt(k)) {
String name =
((Cursor)myListView.getAdapter().getItem(k)).getString(1);
Log.i("XXXX",name + " was selected");
}
}
}
break;
}
}
}
我想将数字作为数组或列表发送。做这个的最好方式是什么?任何帮助或导致正确的道路都受到高度赞赏。
答案 0 :(得分:4)
我在onClick
中使用此代码:
long[] id = getListView().getCheckedItemIds();// i get the checked contact_id instead of position
phoneNumber = new String[id.length];
for (int i = 0; i < id.length; i++) {
phoneNumber[i] = getPhoneNumber(id[i]); // get phonenumber from selected id
}
Intent pickContactIntent = new Intent();
pickContactIntent.putExtra("PICK_CONTACT", phoneNumber);// Add checked phonenumber in intent and finish current activity.
setResult(RESULT_OK, pickContactIntent);
finish();
//
private String getPhoneNumber(long id) {
String phone = null;
Cursor phonesCursor = null;
phonesCursor = queryPhoneNumbers(id);
if (phonesCursor == null || phonesCursor.getCount() == 0) {
// No valid number
signalError();
return null;
} else if (phonesCursor.getCount() == 1) {
// only one number, call it.
phone = phonesCursor.getString(phonesCursor
.getColumnIndex(Phone.NUMBER));
} else {
phonesCursor.moveToPosition(-1);
while (phonesCursor.moveToNext()) {
// Found super primary, call it.
phone = phonesCursor.getString(phonesCursor
.getColumnIndex(Phone.NUMBER));
break;
}
}
return phone;
}
private Cursor queryPhoneNumbers(long contactId) {
ContentResolver cr = getContentResolver();
Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
contactId);
Uri dataUri = Uri.withAppendedPath(baseUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
Cursor c = cr.query(dataUri, new String[] { Phone._ID, Phone.NUMBER,
Phone.IS_SUPER_PRIMARY, RawContacts.ACCOUNT_TYPE, Phone.TYPE,
Phone.LABEL }, Data.MIMETYPE + "=?",
new String[] { Phone.CONTENT_ITEM_TYPE }, null);
if (c != null && c.moveToFirst()) {
return c;
}
return null;
}
您开始onActivityResult
的最后PickContactsActivity
次活动
// TODO Auto-generated method stub
// super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == Constants.REQUEST_CODE_PICK_CONTACT) {
if (data != null) {
String[] temp = data.getStringArrayExtra("PICK_CONTACT");
}
}
}
}
答案 1 :(得分:2)
在startActivityForResult(newActivity)
中使用newActivity
时,您必须拨打setResult(RESULT_OK)
,然后finish()
关闭Activity
。您可以选择在Intent
的调用中添加setResult(RESULT_OK, intent)
。致setResult()
的调用将导致您调用onActivityResult()
的实施,您可以在其中处理Activity
的结果。因此,在您的情况下,您只需创建Intenet
并使用putExtra()
方法之一将数组列表添加到其中。然后,Intent
将传递到onActivityResult()
,您可以在其中提取该信息。有关详细信息,请参阅Intent和Activity。