Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.20 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I get contact name with his/her number
  2. package com.slk.example.CursorActivity;
  3. import android.app.ListActivity;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.os.Bundle;
  7. import android.provider.Contacts.Phones;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.CursorAdapter;
  12. import android.widget.TextView;
  13.  
  14. public class CursorActivity extends ListActivity {
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         Cursor contactsCursor = this.managedQuery(Phones.CONTENT_URI, null, null, null, null);
  19.         this.setListAdapter(new MyContactsAdapter(this,contactsCursor));
  20.     }
  21.  
  22.     private class MyContactsAdapter extends CursorAdapter{
  23.         private Cursor mCursor;
  24.         private Context mContext;
  25.         private final LayoutInflater mInflater;
  26.  
  27.         public MyContactsAdapter(Context context, Cursor cursor) {
  28.             super(context, cursor, true);
  29.             mInflater = LayoutInflater.from(context);
  30.             mContext = context;
  31.         }
  32.  
  33.         @Override
  34.         public void bindView(View view, Context context, Cursor cursor) {
  35.             TextView t = (TextView) view.findViewById(R.id.txtName);
  36.             t.setText(cursor.getString(cursor.getColumnIndex(Phones.NAME)));
  37.  
  38.             t = (TextView) view.findViewById(R.id.txtDisplayName);
  39.             t.setText(cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME)));
  40.  
  41.             t = (TextView) view.findViewById(R.id.txtPhone);
  42.             t.setText(cursor.getString(cursor.getColumnIndex(Phones.NUMBER)));
  43.         }
  44.  
  45.         @Override
  46.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  47.             final View view = mInflater.inflate(R.layout.main, parent, false);
  48.             return view;
  49.         }
  50.     }
  51. }
  52.        
  53. Cursor c = getContentResolver().query(Data.CONTENT_URI,
  54.          new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
  55.                        Data.RAW_CONTACT_ID + "=?" + " AND "
  56.                        + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
  57.                        new String[] {String.valueOf(rawContactId)
  58.          }, null)