加载并显示联系android

时间:2015-10-15 13:45:32

标签: android listview contacts

我想问一下是否有人可以帮助我。 我想将我在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;
    }

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

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

2 个答案:

答案 0 :(得分:1)

你可以获得数字&通过以下代码从您的手机通讯录中输入姓名

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));

        }

答案 1 :(得分:0)

你的最终代码

公共类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("\n\nName: " + name , "Phone No: " + phoneNo);
                }
                pCur.close();
            }
        }
    }

    if(createContact){
            create();
            }   
}
相关问题