カスタム SimpleCursorAdapter を作成します。必要な列についてデータベースにクエリを実行するのに十分賢いと思います。cusror の準備ができたら、Custom Adapter に渡します。
以下のスニペットが役立ちます。
cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
contactList.setAdapter(new MyCursorAdapter(this, R.layout.row, cursor,
new String[] { ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.LAST_TIME_CONTACTED },
new int[] { R.id.name, R.id.email }));
MyCursorAdapter.java
public class MyCursorAdapter extends SimpleCursorAdapter {
private Cursor cursor;
private int layout;
private Context context;
public MyCursorAdapter(Context context, int layout, Cursor cursor,
String[] from, int[] to) {
super(context, layout, cursor, from, to);
this.context = context;
this.cursor = cursor;
this.layout = layout;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
cursor.moveToPosition(position); <---- This is very important.
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(layout, null);
TextView txtName = (TextView) view.findViewById(R.id.name);
TextView txtEmail = (TextView) view.findViewById(R.id.email);
int nameColumnIndex = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int frequencyColumnIndex = cursor
.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED);
txtName.setText(cursor.getString(nameColumnIndex));
txtEmail.setText(cursor.getString(frequencyColumnIndex));
return view;
}
}