当我从未检索到的联系人列表地址数据中选择联系人姓名和地址时

Posted

技术标签:

【中文标题】当我从未检索到的联系人列表地址数据中选择联系人姓名和地址时【英文标题】:When I select contact name and address from contact list address data not retrieved 【发布时间】:2013-08-13 10:09:08 【问题描述】:

我需要从 android 的联系人列表中获取联系人姓名和相关地址。我得到了联系人姓名和他们的电话号码,但很遗憾无法得到地址详细信息。当我选择联系人地址时,它每次都会返回null。

我搜索了谷歌以及 ***。但无法找到解决方案。当我搜索时,我发现地址详细信息在另一个单独的表中,但是我用 id 搜索了那个表,但没有从地址表中返回数据。所以请帮我解决这个问题。提前致谢,

请找到我用来获取联系方式的代码sn-p,

package com.contact.contacts;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

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

        Cursor cur_phone = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        while (cur_phone.moveToNext()) 

            String name = cur_phone
                    .getString(cur_phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            String phoneNumber = cur_phone
                    .getString(cur_phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            String id = cur_phone.getString(cur_phone
                    .getColumnIndex(ContactsContract.Contacts._ID));

            System.out.println("Cursor size : contact Name : "+name);
            System.out.println("Cursor size : contact number : "+phoneNumber);
            System.out.println("Cursor size : id : "+id);

        Cursor cursor_address = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        cursor_address.close();

        // get the data package containg the postal information for the contact
        cursor_address = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
            new String[] StructuredPostal.STREET,
                StructuredPostal.CITY,
                StructuredPostal.POSTCODE,
                ContactsContract.Data.CONTACT_ID + "=? AND " +
                    StructuredPostal.MIMETYPE + "=?",
                new String[]String.valueOf(id), StructuredPostal.CONTENT_ITEM_TYPE,
                null);

        cursor_address.moveToFirst();
        System.out.println("Cursor size : Outside while");

        while (cursor_address.moveToNext()) 
            System.out.println("Cursor size : Inside while");
            // This while statement is not running

        if(cursor_address != null && cursor_address.moveToFirst())
        
            if (cursor_address.getCount() > 0) 
                String Street = cursor_address.getString(cursor_address.getColumnIndex(StructuredPostal.STREET));
                System.out.println("Address : "+Street);
                String Postcode = cursor_address.getString(cursor_address.getColumnIndex(StructuredPostal.POSTCODE));
                String City = cursor_address.getString(cursor_address.getColumnIndex(StructuredPostal.CITY));
            
            else
            
                System.out.println("Cursor is : " + cursor_address);
                Log.i("Contact : ", "Cursor :" + cursor_address);
            
        
        else
        
            System.out.println("Cursor Null : " + cursor_address);
            Log.i("Contact App : ", "Cursor null" + cursor_address);
        
        
        
    

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
        // TODO Auto-generated method stub

    


【问题讨论】:

【参考方案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));
         

【讨论】:

您好 Farha,感谢您的回复,但我想从手机的联系人列表中检索合同地址。但你的答案是从表中获取数据。任何方式都可以回复。 @AhamedSalik-contract 这是什么。它保存在sql数据库或其他地方。 @FarhaSameer786-Sorry 它的联系方式.. 感谢您的回复.. 最后我解决了问题。请在下面找到答案【参考方案2】:

我终于解决了问题,请在下面找到答案,

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
    

【讨论】:

以上是关于当我从未检索到的联系人列表地址数据中选择联系人姓名和地址时的主要内容,如果未能解决你的问题,请参考以下文章

从 iphone 中检索联系人

检索电话号码列表 onActivityResult

使用 android 2.1 在我自己的列表视图中显示姓名和联系电话

android获取联系人图片、姓名和手机号码

在 MS Access 中以分号分隔的列表检索电子邮件地址的子查询

电话号码检索意图