获取从列表视图中选择的联系人的详细信息

2023-12-02

我正在设计应用程序,我希望允许用户选择多个联系人来发送消息。我已使用以下代码成功检索了带有复选框的列表视图中的用户列表。现在我希望当用户单击“完成”按钮时,应在 EDITTEXT 中检索所有选定联系人的电话号码格式如下约翰 、里克 并且所有包含的电话号码仅 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;
}
}

终于配置好了

        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();
            }
        }
    });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取从列表视图中选择的联系人的详细信息 的相关文章

随机推荐