1

I want to get the Contact name of the selected contact in android while working with default contacts of phone. I tried like this but it show all the contact names in the phone but i want only selected contact name

Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phNumber));

                    // query time
                    Cursor cursor = this.getContentResolver().query(contactUri, projection, null, null, null);

                    if(cursor != null) {
                        if (cursor.moveToFirst()) {
                            name =      cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                            Log.v("sai", "Started uploadcontactphoto: Contact Found @ " + phNumber);            
                            Log.v("sai", "Started uploadcontactphoto: Contact name  = " + name);
                        } else {
                            Log.v("sai", "Contact Not Found @ " + phNumber);
                        }
                        cursor.close();
                    }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Sai Durga
  • 593
  • 1
  • 6
  • 17
  • What do you mean by selected contact name? – GrIsHu Nov 12 '13 at 07:50
  • whenever the default contact page is opened in my app the user has to click on one contact.then i want to display that contact details in another activity. – Sai Durga Nov 12 '13 at 08:44
  • If you want to show the details then you can store the details in arraylist and then pass the details into another activity to show there. – GrIsHu Nov 12 '13 at 08:51
  • What details you want to show. – GrIsHu Nov 12 '13 at 08:51
  • can you please post the code for storing the contact details in arraylist.I want to show the all details of the contact – Sai Durga Nov 12 '13 at 09:21

2 Answers2

0

So, you're using the right approach, but you aren't telling Android what specific contact to look up.

You don't state what a "selected contact" is, but I assume you should know its phone number or some such by which to actually query the contact list.

According to documentation, you're just not using the selection parameters.

You can write selection param just as you would an SQL query's WHERE statement. As in ContactsContract.PhoneLookup + " = 123456789"

See this answer for a more detailed explanation.

Community
  • 1
  • 1
velis
  • 8,747
  • 4
  • 44
  • 64
0

Here is the code in which i am storing the email address and contact person name in arraylist

public ArrayList<ContactVo> getNameEmailDetails() {
        ArrayList<ContactVo> m_arrList = new ArrayList<ContactVo>();
        ArrayList<String> emlRecs = new ArrayList<String>();
        HashSet<String> emlRecsHS = new HashSet<String>();
        ContentResolver cr = m_context.getContentResolver();
        String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.PHOTO_ID,
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
        String order = "CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
                + " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
                + ContactsContract.Contacts.DISPLAY_NAME + ", "
                + ContactsContract.CommonDataKinds.Email.DATA
                + " COLLATE NOCASE";
        String filter = ContactsContract.CommonDataKinds.Email.DATA
                + " NOT LIKE ''";
        Cursor cur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION,
                filter, null, order);
        ContactVo m_vo;
        if (cur.moveToFirst()) {
            do {
                m_vo = new ContactVo();
                // names comes in hand sometimes
                String name = cur.getString(1);
                String emlAddr = cur.getString(3);
                System.err.println("Value Is---------->" + cur.getString(2)
                        + "Name--" + name + "Email---" + emlAddr);
                m_vo.setM_sName(name);
                m_vo.setM_sEmail(emlAddr);
                // keep unique only
                if (emlRecsHS.add(emlAddr.toLowerCase())) {
                    emlRecs.add(emlAddr);
                }
                m_arrList.add(m_vo);
            } while (cur.moveToNext());
        }

        cur.close();
        return m_arrList;
    }

EDITED:

Write the below code in your onItemClick event of your listview where you are showing all the contact list.

m_lvList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(m_context, m_arrContact.get(arg2).getM_sEmail(), Toast.LENGTH_LONG).show();
            Intent in = new Intent(m_context, Test.class);
            in.putExtra("pos", arg2);
            in.putParcelableArrayListExtra("parcel", m_arrContact);
            startActivity(in);
        }
    });

Make your vo parcelable to pass the details into another activity and access it.

ContactVo

    public class ContactVo implements Parcelable {
    private String m_sName, m_sEmail;

    public ContactVo() {
    }

    public ContactVo(Parcel in) {
        readFromParcel(in);
    }

    public String getM_sName() {
        return m_sName;
    }

    public void setM_sName(String m_sName) {
        this.m_sName = m_sName;
    }

    public String getM_sEmail() {
        return m_sEmail;
    }

    public void setM_sEmail(String m_sEmail) {
        this.m_sEmail = m_sEmail;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(m_sName);
        dest.writeString(m_sEmail);

    }

    public void readFromParcel(Parcel p_in) {

        m_sName = p_in.readString();
        m_sEmail = p_in.readString();
    }

    public static final Parcelable.Creator<ContactVo> CREATOR = new Parcelable.Creator<ContactVo>() {
        public ContactVo createFromParcel(Parcel s) {
            return new ContactVo(s);
        }

        public ContactVo[] newArray(int size) {
            return new ContactVo[size];
        }
    };

}

In your another activity write as below:

public class Test extends Activity {

    ArrayList<ContactVo> m_arr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout);

        m_arr = getIntent().getParcelableArrayListExtra("parcel");
        int pos = getIntent().getIntExtra("pos", 0);
        System.out.println("Detailsss------>" + m_arr.get(pos).getM_sEmail()
                + m_arr.get(pos).getM_sName());

    }

}

You can add as many details as much you want into the ArrayList<ContactVo> and pass it into another activity.

To get other contact details Check out HERE

GrIsHu
  • 29,068
  • 10
  • 64
  • 102