【问题标题】:Contact Picker Code联系人选择器代码
【发布时间】:2014-01-10 21:25:03
【问题描述】:

我需要一些关于我得到的联系人选择器类的帮助。 该类检索联系人列表并允许我选择一个,但是当我去选择另一个时,它只是替换了第一个。 我想在我的应用中创建一个联系人列表,而不仅仅是一个。

谢谢, 诺姆

代码:

public static final int PICK_CONTACT = 1;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case (PICK_CONTACT): {
        if (resultCode == Activity.RESULT_OK) {
            Uri contentUri = data.getData();
            //Phone Name
            Cursor c = managedQuery(contentUri, null, null, null, null);
            c.moveToFirst();
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            //Phone Number
            String contactId = contentUri.getLastPathSegment();
            Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                    null, Phone._ID + "=?", new String[] { contactId },
                    null);// < - Note, not CONTACT_ID!
            startManagingCursor(cursor);
            Boolean numbersExist = cursor.moveToFirst();
            int phoneNumberColumnIndex = cursor
                    .getColumnIndex(Phone.NUMBER);
            String phoneNumber = "";
            while (numbersExist) {
                phoneNumber = cursor.getString(phoneNumberColumnIndex);
                phoneNumber = phoneNumber.trim();
                numbersExist = cursor.moveToNext();
            }
            stopManagingCursor(cursor);
            //Set
            TextView tv = (TextView) findViewById(R.id.txtSelContact);
            tv.setText(name + "-" + phoneNumber);
        }
        break;
    }
    }
}

}

这里是 on create 函数:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

    //you may fill it here e.g. from your db
    contactList=new ArrayList<String>();

    arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, contactList);

    final ListView lv = (ListView) findViewById(R.id.ContactListView);
    lv.setAdapter(arrayAdapter);

}

这是 layout.xml(出于某种原因,它不允许我发布代码,所以我链接到图像):

https://imagizer.imageshack.us/v2/516x255q90/4/igi5.png

第 29 - 35 行:

btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

【问题讨论】:

    标签: list contacts android-contacts


    【解决方案1】:

    据我所知,您正在将结果写入同一个文本视图:

    //Set
    TextView tv = (TextView) findViewById(R.id.txtSelContact);
    tv.setText(name + "-" + phoneNumber);
    

    您可以在布局中指定一个列表视图

    <ListView
        android:id="@+id/ContactListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
    

    并使用 arrayadapter 将结果添加到此 Listview:

    public class MainActivity extends Activity {
        public static final int PICK_CONTACT = 1;
        private ArrayList<String> contactList;
        private ArrayAdapter<String> arrayAdapter;
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_main);
    
            Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
            btnPickContact.setOnClickListener(new View.OnClickListener() {
                public void onClick(View _view) {
                    Intent intent = new Intent(Intent.ACTION_PICK,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                    startActivityForResult(intent, PICK_CONTACT);
                }
            });
    
            //you may fill it here e.g. from your db
            contactList=new ArrayList<String>();
    
            arrayAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, contactList);
    
            final ListView lv = (ListView) findViewById(R.id.contactListView);
            lv.setAdapter(arrayAdapter);
    
        }
    
    
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            switch (requestCode) {
            case (PICK_CONTACT): {
                if (resultCode == Activity.RESULT_OK) {
                    Uri contentUri = data.getData();
                    //Phone Name
                    Cursor c = managedQuery(contentUri, null, null, null, null);
                    c.moveToFirst();
                    String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    //Phone Number
                    String contactId = contentUri.getLastPathSegment();
                    Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                            null, Phone._ID + "=?", new String[] { contactId },
                            null);// < - Note, not CONTACT_ID!
                    startManagingCursor(cursor);
                    Boolean numbersExist = cursor.moveToFirst();
                    int phoneNumberColumnIndex = cursor
                            .getColumnIndex(Phone.NUMBER);
                    String phoneNumber = "";
                    while (numbersExist) {
                        phoneNumber = cursor.getString(phoneNumberColumnIndex);
                        phoneNumber = phoneNumber.trim();
                        numbersExist = cursor.moveToNext();
                    }
                    stopManagingCursor(cursor);
                    //Set
                    arrayAdapter.add(name + "-" + phoneNumber);
                    arrayAdapter.notifyDataSetChanged();
                }
                break;
            }
            }
        }
    }
    

    【讨论】:

    • 谢谢,它有效。我真正需要在这里添加的另一件事是能够在我的应用程序中通过该活动呼叫、发送消息或通过电子邮件发送联系人。我该怎么做?
    • 出了点问题,当我在不同的库上自行启动活动时,它可以工作,但是当我将它添加到我的应用程序库并通过应用程序打开活动时,它崩溃了。我能做什么?
    • 这是我在崩溃时得到的 LogCat:imagizer.imageshack.us/v2/1240x596q50/15/64j9.png
    • 你能把你的 work_contacts.java 发布到 create 函数上吗?
    • 您的布局中的按钮和列表是否正确命名?你也可以发布你的布局xml吗?
    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 2017-02-05
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多