加载并显示Contact android



我想问一下是否有人可以帮助我。我想加载联系人从我的联系人列表在android到我的视图作为文本或在一个列表视图。我有一个按钮,说"添加联系人"按钮名称是addcontacts。我有一个ContactView活动,我已经设置了一切。我已经编码了一些东西,但那不是我想做的。

public class ContactView extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_view);
        Button insert = (Button) findViewById(R.id.addcontact);
        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                insert();
            }
        });
    }
    public void insert() {
        Intent intent = new Intent(
                ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
                ContactsContract.Contacts.CONTENT_URI);
        intent.setData(Uri.parse("tel:011-9999999"));//specify your number here
        intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Google");
        intent.putExtra(ContactsContract.Intents.Insert.POSTAL,
                "Addresse, Street Name, State/Country");
        startActivity(intent);
        Toast.makeText(this, "Lade Ansicht", Toast.LENGTH_SHORT).show();
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contact_menu, menu);
        return true;
    }

这是我已经有的。这让我可以在我的应用程序中创建联系人,但这不是它应该的方式,因为我想显示已经创建的联系人。当我允许用户在应用程序中创建它们时,它们将具有双重联系人。

有人能帮我从现有的联系人加载它们,并以我的方式显示它们吗?

你可以得到number &通过以下代码

从您的电话联系人中命名
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
        String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
        String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }

你的最终代码

公共类ContactView扩展AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    Button insert = (Button) findViewById(R.id.addcontact);
    insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            scanContacts();
        }
    });
}
public void insert() {
    Intent intent = new Intent(
            ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
            ContactsContract.Contacts.CONTENT_URI);
    intent.setData(Uri.parse("tel:011-9999999"));//specify your number here
    intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Google");
    intent.putExtra(ContactsContract.Intents.Insert.POSTAL,
            "Addresse, Street Name, State/Country");
    startActivity(intent);
    Toast.makeText(this, "Lade Ansicht", Toast.LENGTH_SHORT).show();
}
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.contact_menu, menu);
    return true;
}


private void scanContacts() {
    boolean createContact = true;
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);

                while (pCur.moveToNext()) {
                    //Your condition here to check the input number is already in contact list 
                    if(your_condition){
                        createContact = false;//
                    }       

                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.d("nnName: " + name , "Phone No: " + phoneNo);
                }
                pCur.close();
            }
        }
    }
    if(createContact){
            create();
            }   
}

最新更新