Skip to content Skip to sidebar Skip to footer

When I Select Contact Name And Address From Contact List Address Data Not Retrieved

I need to get contact name and relevant address from contact list in android. I get the contact name and their phone number, but unfortunately unable to get the address details. Wh

Solution 1:

public ArrayList<String> getName(){

        ArrayList<String> mdetail=new ArrayList<String>();

        SQLiteDatabase db = this.getWritableDatabase();
        String selectQuery = "SELECT "+COLUMN_PERSON_NAME+" from "+TABLE_DETAIL;

        Cursor mCursor = db.rawQuery(selectQuery, null);

        if(mCursor.moveToFirst()){          
            do {
                mdetail.add(mCursor.getString(mCursor.getColumnIndex(COLUMN_PERSON_NAME)));

            } while (mCursor.moveToNext());
        }
        mCursor.close();
        db.close();
        return mdetail;
    }



if Your data is Sucessfully added in your database just use this code to retreive from database and show in list view like this....


if (!db.exists()) {
         } else {
             mListName=baseManager.getName();
             listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mListName));
         }

Solution 2:

Finally I solve the problem, Please find the below answer,

package com.contact.contacts;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

    private ListView listView;
    private List<ContactBean> list = new ArrayList<ContactBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.list);
        listView.setOnItemClickListener(this);


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

        String phone = null;
        String poBox = null;
        String street = null;
        String city = null;
        String state = null;
        String postalCode = null;
        String country = null;

        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            //Query for phone 
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                    null, 
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]{id}, null);
                    while (pCur.moveToNext()) {
                    // Get the phone number
                        phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    } 
                    pCur.close();

            }

        String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
        String[] addrWhereParams = new String[]{id, 
            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 
        Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                    null, addrWhere, addrWhereParams, null); 
        while(addrCur.moveToNext()) {
            poBox = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
            street = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
            city = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
            state = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
            postalCode = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
            country = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
            String type = addrCur.getString(
                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
        } 
        addrCur.close();

        ContactBean objContact = new ContactBean();
        objContact.setName(name);
        objContact.setPhoneNo(phone);

        objContact.setAddressLine1(poBox);
        objContact.setAddressLine2(street);
        objContact.setCity(city);
        objContact.setPostalCode(postalCode);
        objContact.setCountry(country);
        list.add(objContact);

        ContactAdapter objAdapter = new ContactAdapter(ContactActivity.this,
                R.layout.single_contact, list);
        listView.setAdapter(objAdapter);

        if (null != list && list.size() != 0) {
            Collections.sort(list, new Comparator<ContactBean>() {

                @Override
                public int compare(ContactBean lhs, ContactBean rhs) {
                    return lhs.getName().compareTo(rhs.getName());
                }
            });

        } else {
            showToast("No Contact Found!!!");
        }
    }

        }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> listview, View v, int position,
            long id) {
        // Event for item click
    }
}

Post a Comment for "When I Select Contact Name And Address From Contact List Address Data Not Retrieved"