如何在弹出窗口中显示联系人?

时间:2014-06-21 11:57:11

标签: android popup contacts

我知道他们有许多联系人列表参考,但我需要的是在弹出菜单中获取联系人。

当用户点击按钮弹出窗口应该显示联系人列表时,他应该能够选择多个朋友的姓名和联系人号码存储它们。

我已经使用了下面的代码,并且仅通过保存弹出窗口的信息获取并尝试完全按照我们的移动联系人列表视图。

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;

import android.widget.TextView;

public class MainActivity extends Activity {
public TextView outputText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputText = (TextView) findViewById(R.id.textView1);
fetchContacts();

}


public void fetchContacts() {

String phoneNumber = null;
String email = null;

Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

Uri EmailCONTENT_URI =  ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;

StringBuffer output = new StringBuffer();

ContentResolver contentResolver = getContentResolver();

Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);  

// Loop for every contact in the phone
if (cursor.getCount() > 0) {

while (cursor.moveToNext()) {

String contact_id = cursor.getString(cursor.getColumnIndex( _ID );
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME );
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER ));
if (hasPhoneNumber > 0) {
output.append("\n First Name:" + name);

// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);

while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
output.append("\n Phone number:" + phoneNumber);
}

phoneCursor.close();

// Query and loop for every email of the contact
Cursor emailCursor = contentResolver.query(EmailCONTENT_URI,    null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null);

while (emailCursor.moveToNext()) {

email = emailCursor.getString(emailCursor.getColumnIndex(DATA));

output.append("\nEmail:" + email);


}

emailCursor.close();
}

output.append("\n";
}

outputText.setText(output);
}
}

}

如果有人对此有任何想法,请帮助我的朋友。

1 个答案:

答案 0 :(得分:0)

首先创建一个按钮,然后单击该按钮

final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.email_dialog);//

        ListView lv;
        lv= (ListView ) dialog.findViewById(R.id.list);//this is your list xml 

使用多选列表视图适配器并使用上面的代码

填充列表视图中的联系人