1

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

3 回答 3

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();
            }
        }
    });
于 2012-09-22T05:01:00.513 回答
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);
于 2012-09-09T13:32:29.320 回答
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 类型的数组列表。您将获得所有选定的名称和数字。现在,您可以根据需要在“编辑文本”中显示它们。

我想这会对你有所帮助。

于 2012-09-21T09:59:52.947 回答