bfmv991
bfmv991

Reputation: 109

Load and display Contact android

I wanted to ask if someone can help me. I'd like to load a contact from my contactlist in android to my view as text or in a listview. I have a button that says "Add Contacts" the Button Name is addcontacts. I have a ContactView Activity where i already Setup everything. I've coded something already but thats not what i want to do.

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

This is what i already have. This lets me create Contacts inside my App but thats not the way it should be because i want to display Contacts that are already created. When i let the user create them within the app they'll have double Contacts.

Can someone help me load them from existing Contacts and display them in my way??

Upvotes: 1

Views: 160

Answers (2)

Aks4125
Aks4125

Reputation: 5720

your final code

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

Upvotes: 0

Zahan Safallwa
Zahan Safallwa

Reputation: 3914

You can get number & name from your phone contacts by following code

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

        }

Upvotes: 1

Related Questions