Below is my Contact_Picker class. I am going to use this class to create a checklist of contacts, giving the user the ability to select multiple contacts from the phone book. I have an xml layout that I use that has two buttons at the bottom: Clear All and Done.
When the "Finish" button is clicked, I need to get all the names that are marked and save them in a list / preference file. Right now, I can find which POSITIONS are checked, but I donβt know how to get the relevant information related to them (name / phone number of the selected contact). I searched for days on a method that would work, and didn't come up with anything. Any code / pseudo-code / ideas are greatly appreciated.
Contact_Picker Class:
public class Contact_Picker extends ListActivity { protected static final String TAG = null; public String[] Contacts = {}; public int[] to = {}; public ListView myListView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contacts_list); final Button done_Button = (Button) findViewById(R.id.done_Button); final Button clear_Button =(Button) findViewById(R.id.clear_Button); Cursor mCursor = getContacts(); startManagingCursor(mCursor); ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, mCursor, Contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME }, to = new int[] { android.R.id.text1 }); setListAdapter(adapter); myListView = getListView(); myListView.setItemsCanFocus(false); myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); clear_Button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(getApplicationContext(),"Selections Cleared", Toast.LENGTH_SHORT).show(); ClearSelections(); } }); done_Button.setOnClickListener(new View.OnClickListener() { public void onClick (View v){ Log.i(TAG,":Done Button Selected:"); SparseBooleanArray checkedPositions = myListView.getCheckedItemPositions(); Log.i(TAG,"Number of Checked Positions: " + checkedPositions.size()); if (checkedPositions != null) { int count = myListView.getCount(); for ( int i=0;i<count;i++) { Log.i(TAG,"Selected items: " + checkedPositions.get(i)); } } } });
Create output such as:
Sele02-12 01:25:09.733: INFO/(219): :Done Button Selected: 02-12 01:25:09.743: INFO/(219): Number of Checked Positions: 2 02-12 01:25:09.743: INFO/(219): Selected items: true 02-12 01:25:09.743: INFO/(219): Selected items: false 02-12 01:25:09.743: INFO/(219): Selected items: true 02-12 01:25:09.752: INFO/(219): Selected items: false
source share