Contact List of Multiple Contacts [getCheckedItemPositions ()]

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; /** Called when the activity is first created. */ @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(); } }); /** When 'Done' Button Pushed: **/ 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)); } } } }); //<-- End of Done_Button } //<-- end of onCreate(); private void ClearSelections() { int count = this.myListView.getAdapter().getCount(); for (int i = 0; i < count; i++) { this.myListView.setItemChecked(i, false); } } private Cursor getContacts() { // Run query Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; return managedQuery(uri, projection, selection, selectionArgs, sortOrder); } //<-- end of getContacts(); } 

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 
+4
source share
4 answers

Here is the correct approach:

 SparseBooleanArray selectedPositions = listView.getCheckedItemPositions(); for (int i=0; i<selectedPositions.size(); i++) { if (selectedPositions.get(selectedPositions.keyAt(i)) == true) { //do stuff } } 
0
source

see this

  public String[] getlistcontacts() { // TODO Auto-generated method stub int i=0; ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null); Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null , null); int a= cur.getCount(); String[] cttlist=new String[a+1]; cur.moveToFirst(); pCur.moveToFirst(); for (int j=0; j<a;j++){ int nm=cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); //int nb=pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String name=cur.getString(nm); int nb=pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String number=pCur.getString(nb); cttlist[i]=name.concat("<;>").concat(number); //Toast.makeText(PizzastimeActivity.this, "alkamkljziha"+name+":"+number, Toast.LENGTH_LONG).show(); i++; cur.moveToNext(); pCur.moveToNext(); } return cttlist; } 

in this code, I tried to get the contact list in the row table, then you can easily use it

+1
source

perhaps you can manually track your contacts:

  Vector<String> names=new Vector<String>(); private Cursor getContacts() {... Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder); int col = cur.getColumnIndex("display_name"); while(cur.moveToNext()) names.add(cur.getString(col)); cur.moveToFirst(); return cur; } 

and then output them synchronously:

 for ( int i=0;i<count;i++) { Log.i(TAG,"Selected items: " + checkedPositions.get(i)); Log.i(TAG,"Selected name: " + names.get(i)); } 
0
source

I forgot about this message until the Pannous team responded. I'm sure their method will work, but in the end I used this:

  SparseBooleanArray checked = myListView.getCheckedItemPositions(); for (int i = 0; i < ContactsList.length; i++) { Log.v(TAG, ContactsList[i] + ": " + checked.get(i)); //<-- Will print every contact with 'true' if (checked.get(i) == true) { Object o = getListAdapter().getItem(i); String name = o.toString(); WriteSettings(self, name); } } 

Just in case, anyone has a problem with a list with several choices.

0
source

Source: https://habr.com/ru/post/1339451/


All Articles