0

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?

user1593869
  • 37
  • 1
  • 7

1 Answers1

0

I think the best way to share data through your application is to use a Singleton which contain all your data. Here is a good tutorial:

http://www.roseindia.net/java/beginners/SingletonPattern.shtml

You can also use SharedPreferences if you want persistance between launches of your app:

http://developer.android.com/reference/android/content/SharedPreferences.html

Aerilys
  • 1,628
  • 1
  • 16
  • 22
  • Sorry I think I may have misled you - I mean share the data with other *activities* as in activities within my application. Thanks for the suggestion though – user1593869 Mar 24 '13 at 18:18
  • I already use sharedpreferences for certain items, but is this a suitable option for storing an array (or other object?) of names and numbers? Isn't sharedpreferences only for simple namevaluepairs? – user1593869 Mar 24 '13 at 19:23
  • You can still serialize your arrays into json, and then deserialize when you need it on a new launch. – Aerilys Mar 24 '13 at 19:25
  • Brilliant - I will use sharedpreferences then. The main problem I am encountering though is this code seems to just return ALL the contacts, not just the selected ones. I cant figure out what ive missed – user1593869 Mar 24 '13 at 20:06
  • Aerilys -- Could you please advise re. the above? I appreciate what you've said already, but I am desperate to resolve the issue in its entirety. Thank you – user1593869 Mar 24 '13 at 22:30
  • You may for example generate a listview with retrieved contacts, and propose an option (on long click for instance) to add them. Or maybe the best solution is to pick contacts from an intent. See that post http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list – Aerilys Mar 24 '13 at 22:45
  • Thank you. Isn't the contact picker method deprecated, or am i confused? I would like to amend the code i already have if possible, rather than recoding. Thanks again – user1593869 Mar 24 '13 at 23:28
  • Aerilys - Please note in my question and subsequent i refer to contacts - in fact you discuss a serialised array of contacts. This is not possible using the contact picker, a custom listview is required. – user1593869 Mar 25 '13 at 02:57
  • I think you're misunderstanding the concept of contact picker. It's not deprecated. And when you pick a contact from the contact picker, you retrieve data you choose, and store them in a custom class. So your serializable array is an array of this custom class (or more probably a list). Then, you can use this array to display selected contacts, using a listview with an appropriate adapter. – Aerilys Mar 25 '13 at 11:49
  • I was mistaken about deprecation but you can still only use the contact picker method to retrieve one contact at a time. Thank you for the suggestion though. – user1593869 Mar 25 '13 at 14:20