How can I get contact name with his/her number

走远了吗. 提交于 2019-12-03 09:53:31

This is a common problem with layers of abstraction. The layer doesn't abstract a specific functionality, tool, or case you'd like to use. In this case, however, it appears that not all hope is lost. It appears that the android scripting api is an open-source project. Why not contribute a patch that would provide this ability to the project?

I may contribute such a patch at some point in the future, but if it's important to you, you could do the same before I do, and be on your way!

RobinHood
package com.slk.example.CursorActivity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class CursorActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor contactsCursor = this.managedQuery(Phones.CONTENT_URI, null, null, null, null);
        this.setListAdapter(new MyContactsAdapter(this,contactsCursor));
    }

    private class MyContactsAdapter extends CursorAdapter{
        private Cursor mCursor;
        private Context mContext;
        private final LayoutInflater mInflater;

        public MyContactsAdapter(Context context, Cursor cursor) {
            super(context, cursor, true);
            mInflater = LayoutInflater.from(context);
            mContext = context;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView t = (TextView) view.findViewById(R.id.txtName);
            t.setText(cursor.getString(cursor.getColumnIndex(Phones.NAME)));

            t = (TextView) view.findViewById(R.id.txtDisplayName);
            t.setText(cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME)));

            t = (TextView) view.findViewById(R.id.txtPhone);
            t.setText(cursor.getString(cursor.getColumnIndex(Phones.NUMBER)));
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            final View view = mInflater.inflate(R.layout.main, parent, false);
            return view;
        }
    }
}

You might want to take a look at the ContactsContract data table. Query it with something like this:

    Cursor c = getContentResolver().query(Data.CONTENT_URI,
         new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
                       Data.RAW_CONTACT_ID + "=?" + " AND "
                       + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
                       new String[] {String.valueOf(rawContactId)
         }, null)

You may try, if didn't yet, the Contact API : http://developer.android.com/resources/articles/contacts.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!