Using the following code I am populating a listview with contacts' names and a corresponding checkbox.
At the moment, the code just spits the whole list into the log.
I intend to take selected contacts from the list and store them locally so they can be used in the app in other activities.
Please could you offer some guidance?
I have looked at the android documentation as well as numerous posts on here. I now seek your kind assistance in this matter.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.nominatecontactsactivitytest);
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cursor, columns, to);
this.setListAdapter(adapter);
Button finishButton = (Button) this.findViewById(R.id.finishButton);
finishButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SimpleCursorAdapter adapter = (SimpleCursorAdapter) nominateContactsActivity.this.getListAdapter();
Cursor cursor = adapter.getCursor();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
ListView lv = nominateContactsActivity.this.getListView();
SparseBooleanArray selectedItems = lv.getCheckedItemPositions();
for (int i = 0; i < selectedItems.size(); i++) {
int selectedPosition = selectedItems.keyAt(i);
cursor.moveToPosition(selectedPosition);
strName=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
strTelNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("NAME: ",strName);
Log.d("NUM: ", strTelNo);
}cursor.close();
}
});
}
Thank you very much.
EDIT:
Ok, so I will use sharedpreferences to store a serialised array of contacts.
I still face the problem of actually generating this array though - can anyone please help?