<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "
/>
<TextView android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone: "
/>
<TextView android:id="@+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
public class ListContacts extends ListActivity {
private SimpleCursorAdapter myAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
// start mappings
String[] columns = new String[] {People.NAME, People.NUMBER};
int[] names = new int[] {R.id.contact_name, R.id.phone_number};
myAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, columns, names);
setListAdapter(myAdapter);
}
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
Intent intent = new Intent(Intent.ACTION_CALL);
Cursor cursor = (Cursor) myAdapter.getItem(position);
long phoneId = cursor.getLong(cursor.getColumnIndex(People.PRIMARY_PHONE_ID));
intent.setData(ContentUris.withAppendedId(Phones.CONTENT_URI, phoneId));
startActivity(intent);
}
}
这里一定要注意 myAdapter.getItem(position);取出来的对象是cursor
publicclass MySimpleCursorAdapter extends{
privateCursor _cursor;
privateContext _context;
public(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
_cursor = c;
_context = context;
}
/**
* {@inheritDoc}
*/
@Override
publicvoid bindView(View view, Context context, Cursor cursor) {
ImageView imageView = (ImageView) view.findViewById(R.id.contact_image);
int id = _cursor.getColumnIndex(People._ID);
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, _cursor.getLong(id));
Bitmap bitmap = People.loadContactPhoto(_context, uri, R.drawable.icon, null);
imageView.setImageBitmap(bitmap);
super.bindView(view, context, cursor);
}
}