Reputation: 851
I've a problem and don't know the way out of it at the moment. I hope someone could help me with my issue.
I want to let the user check, which contacts he want to add to a preference. The preference is needed for sending sms to the given contacts. I initialize my MultiSelectListPreference in a xml like this:
<MultiSelectListPreference
android:key="sourcecontacts"
android:entries="@array/contacts"
android:entryValues="@array/contacts"
android:title="@string/source_contact"
/>
In my Activity I have an OnPreferenceClickListener which should call the method manageContacts() if the MultiSelectListPreference is clicked.
In this method I want to read any contacts of the device and put them into the MultiSelectListPreference that the user can mark those contacts, which should be inserted into the SharedPrefs.
Now my problem is, that I add the entries to the ListPreference but in the UI I can't see any entries.
Do I have to reload the View or the component?
I look forward to helpful answers.
You'll find my manageContact method below:
private void manageContacts() {
if (entryValues == null || entries == null || entries.length <= 1
|| entryValues.length <= 1) {
Cursor contacts = getContentResolver().query(Contacts.CONTENT_URI,
null, null, null, Contacts.DISPLAY_NAME);
startManagingCursor(contacts);
entryValues = new CharSequence[contacts.getCount()];
entries = new CharSequence[contacts.getCount()];
int ix = 0;
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone._ID }, null,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String contactNumber = cursor
.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName = cursor
.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (contactNumber != null && !contactNumber.isEmpty()) {
entries[ix] = contactName;
entryValues[ix] = contactNumber;
ix++;
}
cursor.moveToNext();
}
cursor.close();
cursor = null;
}
MultiSelectListPreference sourceContacts = (MultiSelectListPreference)
getPreferenceScreen().findPreference("sourcecontacts");
MultiSelectListPreference targetContacts = (MultiSelectListPreference)
getPreferenceScreen().findPreference("targetcontacts");
if (entries != null && entryValues != null && entries.length >= 1
&& entryValues.length >= 1) {
sourceContacts.setSelectable(true);
targetContacts.setSelectable(true);
sourceContacts.setEntries(entries);
sourceContacts.setEntryValues(entryValues);
targetContacts.setEntries(entries);
targetContacts.setEntryValues(entryValues);
} else {
sourceContacts.setSelectable(false);
targetContacts.setSelectable(false);
}
}
Upvotes: 1
Views: 949