android - 多个联系人选择器列表

标签 android checkbox android-listview android-contacts contactscontract

我有一个联系人选择器列表,其中包含具有电话号码的联系人的复选框。 现在,我的问题是似乎无法获取所检查的联系人的姓名和电话号码。

这是我的代码:

    import android.app.ListActivity;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.util.Log;
    import android.util.SparseBooleanArray;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;

    public class Create_Group extends ListActivity implements OnClickListener{

        // List variables
        public String[] Contacts = {};
        public int[] to = {};
        public ListView myListView;

        Button save_button;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_group);

            // Initializing the buttons according to their ID
            save_button = (Button)findViewById(R.id.save_group_button);

            // 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.save_group_button:

                int checked_Names_Counter = 0;

                // Goes over the list of contacts and checks which were checked
                for (int j = 0; j < myListView.getCount(); j++)
                {
                    if (myListView.isItemChecked(j) == true)
                    {   
                        Cursor cur = getContacts();
                        ContentResolver contect_resolver = getContentResolver();
                        cur.moveToFirst();

                        /**
                        * Here I tried to compare the IDs but each list has different IDs so it didn't really help me...
                        // Converts the current checked name ID into a String
                        String Checked_ID = String.valueOf(myListView.getCheckedItemIds()[checked_Names_Counter]);

                        // Checks if the current checked ID matches the cursors ID, if not move the cursor to the next name
                        while (Checked_ID != cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID)))
                        {
                            cur.moveToNext();
                        }
                        */

                        /**
                        * Here I tried to compare the names, even though it's not a good pratice, and it didn't work either...
                        String Checked_Name = myListView.getAdapter().getItem(checked_Names_Counter).toString();

                        // Checks if the current checked ID matches the cursors ID, if not move the cursor to the next name
                        while (Checked_Name != cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)))
                        {
                            cur.moveToNext();
                        }
                        */
                        String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                        String name = "";
                        String no = "";

                        Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);


                        name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        id = null;
                        name = null;
                        no = null;
                        phoneCur = null;
                        checked_Names_Counter++;

                    }
                }

                // Goes back to the Manage Groups screen
                i = new Intent(this, Manage_Groups.class);
                startActivity(i);
                break;  
        }

      }
    }

有什么想法吗?

谢谢!!

最佳答案

看来你已经很接近了,我使用 ListView.getCheckedItemIds() 返回所选联系人的唯一 ID:

public void onClick(View view) {
    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);

        // Do whatever you want with the data
    }
}

添加

我有一个关于这段代码的简单问题:

// Goes back to the Manage Groups screen
i = new Intent(this, Manage_Groups.class);
startActivity(i);

这是否会让用户返回到之前的 Activity?如果是这样,您应该使用 finish(); 代替。 finish() 结束当前 Activity,将其从堆栈中取出并释放所有内存(浪费的内存越少意味着应用程序速度越快。)它还允许前一个 Activity 在离开时恢复保存的状态(填充 EditTexts、之前的 Spinner 选择) 、切换按钮和复选标记等) Activity 将从用户停止的地方恢复。

关于android - 多个联系人选择器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11283831/

相关文章:

java - Android获取谷歌地图的最后已知位置

java - 从一个类中更改另一个类中的字符串。安卓

android - 如何从列表中删除复选框项目

android-layout - 首次启动时的帮助屏幕(9 补丁)

android - 在 android 中转换时在 onstatusUpdated() 中获取媒体状态时出现空指针异常

android - 控制复选框状态

javascript - 如何检查复选框的值是否存在于文本区域中

android - 从 Activity2 传递到 Activity1,然后再次反射(reflect)从 Activity 1 到 Activity 2 的更改

java - Android 返回旧的 Activity

android - 将 Listview 与布局的其他元素对齐