Para entendernos mejor, en primer lugar comparto el código para que lo ejecuten y luego empecemos a analizar paso a paso las herramientas :
MainActivity.java
public class MainActivity extends Activity { // TextView textDetail; private ListView listView; private List<Contacto> list = new ArrayList<Contacto>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list); listView.setAdapter(new AdapterContacto(getlistContactos(), this)); } public List getlistContactos(){ List<Contacto> list = new ArrayList<Contacto>();
int count=0;Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (phones.moveToNext()) { count++; String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Contacto objContact = new Contacto(); objContact.setName(name); objContact.setPhoneNo(phoneNumber); list.add(objContact); } phones.close(); System.out.println(count); return list; }
}
Contacto.java
public class Contacto { private String name; private String phoneNo; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNo() { return phoneNo; } public void setPhoneNo(String phoneNo) { this.phoneNo = phoneNo; } }AdapterContacto.javapublic class AdapterContacto extends BaseAdapter { private List<Contacto> list; Context context; AdapterContacto (List<Contacto> list, Context context){ this.list = list; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.listcontacto, null); } TextView contactName = (TextView)v.findViewById(R.id.tvname); TextView contactPhone = (TextView)v.findViewById(R.id.tvphone); Contacto msg = list.get(position); contactName.setText(msg.getName()); contactPhone.setText(msg.getPhoneNo()); return v; } }Dentro de la carpeta RES/layout:
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" > </ListView> </LinearLayout>listcontacto.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tvname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginTop="3dp" android:ellipsize="end" android:maxLines="1" android:text="Title" android:textColor="#000000" android:textSize="17dp" android:textStyle="bold" > </TextView> <TextView android:id="@+id/tvphone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tvname" android:layout_marginLeft="3dp" android:layout_marginTop="10dp" android:ellipsize="end" android:maxLines="1" android:text="Phone no" android:textColor="#000000" android:textSize="14dp" > </TextView> </RelativeLayout>Por último, para que todo esto funcione, debemos activar los permiso para la lectura de datos guardados en el smartphone:
Manifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>Al finalizar, tendremos lo siguiente:(imagen referencial, en otro post les compartiré como cargar las fotos)