我正在设计应用程序,我希望允许用户选择多个联系人来发送消息。我已使用以下代码成功检索列表视图中的用户列表。现在我希望当用户点击“完成”按钮时,所有选定联系人的 PHONE NUMBER应以EDITTEXT 检索,格式如 John< + 919898xxxxxx>,Rick&lt ; + 919988xxxxxx> 以及包含仅10位数字即“9898xxxxxx”的所有电话号码应自动存储在以逗号分隔的字符串中(9898xxxxxx,9988xxxxxx)。我怎样才能达到要求。
public class ContactsActivity extends ListActivity {
protected static final String TAG = null;
public String[] Contacts = {};
public int[] to = {};
public ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
final Button done_Button = (Button) findViewById(R.id.done_Button);
final Button clear_Button =(Button) findViewById(R.id.clear_Button);
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);
clear_Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Selections Cleared", Toast.LENGTH_SHORT).show();
ClearSelections();
}
});
/** When 'Done' Button Pushed: **/
done_Button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
Log.i(TAG,":Done Button Selected:");
SparseBooleanArray selectedPositions = myListView.getCheckedItemPositions();
Log.i(TAG,"Number of Checked Positions: " + selectedPositions.size());
for (int i=0; i<selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
//do stuff
}
}
}
});
}
private void ClearSelections() {
int count = this.myListView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.myListView.setItemChecked(i, false);
}
}
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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_contacts, menu);
return true;
}
}
答案 0 :(得分:2)
最后配置
done_Button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
String name = null;
String number = null;
long [] ids = myListView.getCheckedItemIds();
for(long id : ids) {
Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
while(contact.moveToNext()){
name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//name+=name;
number = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//number+=number;
}
Toast.makeText(getApplicationContext(), "Name: " +name + "\n" + "Number: " + number , Toast.LENGTH_LONG).show();
}
}
});
答案 1 :(得分:0)
String numberListString = "";
for (int i=0; i<selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
//do stuff
numberListString = numberListString + "," + numberAtCurrentSelectedPostion;
}
}
mEditText.setText(numberListString);
答案 2 :(得分:0)
按下完成按钮尝试此操作: -
done_Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name.clear();
number.clear();
Log.i(TAG, ":Done Button Selected:");
SparseBooleanArray selectedPositions = myListView
.getCheckedItemPositions();
Log.i(TAG,
"Number of Checked Positions: "
+ selectedPositions.size());
Cursor cur = getContacts();
for (int i = 0; i < selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
// do stuff
cur.moveToPosition(selectedPositions.keyAt(i));
name.add(cur.getString(1));
}
}
for (int i = 0; i < name.size(); i++) {
Cursor lCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, "DISPLAY_NAME = ? ",
new String[] { name.get(i) }, null);
lCursor.moveToFirst();
number.add(lCursor.getString(lCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
lCursor.close();
}
}
});
其中name和number是String类型的数组列表。您将获得所有选定的名称和数字。现在,您可以根据需要在“编辑文本”中显示它们。
我认为这会对你有帮助。