Here, you can find the example of checked textview in the listview and read all email address in the android phone
1. Design the main Layout & row Layout with Checked TextView
Main.xml< ListView android:id="@+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff" />
Row.xml< CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView1" android:paddingLeft="20dip"
android:paddingRight="20dip" android:paddingTop="10dip"
android:paddingBottom="10dip" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:checkMark="@android:drawable/checkbox_off_background"
android:textColor="#000000" />
2. Create the CursurAdpater
private class ContactListAdapter extends CursorAdapter {
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String o = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
CheckedTextView tt = (CheckedTextView) view
.findViewById(R.id.checkedTextView1);
tt.setText(o);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
return v;
}
}3. set the Contact Cursor to the ContactListAdapter
ContentResolver cr = getContentResolver();
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,
null, null);
l.setAdapter(new ContactListAdapter(this, emails));4. Change the State while select the Item
l.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View v, int arg2,
long arg3) {
CheckedTextView tt = (CheckedTextView) v
.findViewById(R.id.checkedTextView1);
if (!tt.isChecked()) {
tt.setChecked(true);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
} else {
tt.setChecked(false);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}
}
});
5. Add the Contact Read Permission in androidmanifest.xml
< uses-permission android:name="android.permission.READ_CONTACTS" />Sample Program :public class ContactListingActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView l = (ListView) findViewById(R.id.listView1);
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,
null, null);
l.setAdapter(new ContactListAdapter(this, emails));
l.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View v, int arg2,
long arg3) {
CheckedTextView tt = (CheckedTextView) v
.findViewById(R.id.checkedTextView1);
if (!tt.isChecked()) {
tt.setChecked(true);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
} else {
tt.setChecked(false);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}
}
});
}
private class ContactListAdapter extends CursorAdapter {
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String o = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
CheckedTextView tt = (CheckedTextView) view
.findViewById(R.id.checkedTextView1);
tt.setText(o);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
return v;
}
}
}