overriden getView() in customized SimpleCursorAdapter does not show content on screen for CheckedTextView
-
04-10-2022 - |
Frage
I need to programmatically check or uncheck contact list based on user selected contact group. Based on some suggestions I got from this forum, I should implement the logic in either getView()
or bindView()
in my adaptor which extends SimpleCursorAdapter
. However, the code shown below does not seem to work. The contact list on screen is blank. Could you tell me what I did wrong?
public View getView(int position, View convertView, ViewGroup viewGroup) {
ContactViewHolder holder = null;
CheckedTextView tv = null;
if (convertView == null) {
int layout = android.R.layout.simple_list_item_multiple_choice;
convertView = mInflater.inflate(layout, viewGroup, false);
holder = new ContactViewHolder();
tv = (CheckedTextView) convertView.findViewById(android.R.id.text1);
holder.checkedTextView = tv;
holder.position = position;
holder.displayName = contactList.get(position);
holder.checked = tv.isChecked();
convertView.setTag(holder);
contactListMap.put(position, holder);
} else {
holder = (ContactViewHolder) convertView.getTag();
}
ContactViewHolder value = contactListMap.get(position);
holder.checkedTextView = value.checkedTextView;
// holder.checkedTextView.setChecked(value.checked);
return convertView;
}
public class ContactViewHolder {
public String contactId;
public String displayName;
public int position;
public boolean checked;
public CheckedTextView checkedTextView;
}
Code snippet for Loading contacts inside fragment class which extends ListFragment implements LoaderCallbacks
private static final String[] PROJECTION = {Contacts._ID, // _ID is always required Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display };
// and name should be displayed in the text1 textview in item layout
private static final String[] FROM = {Contacts.DISPLAY_NAME_PRIMARY};
private static final int[] TO = {android.R.id.text1};
private static String SORT_ORDER = Contacts.SORT_KEY_PRIMARY;
private static String SELECTION = Contacts.DISPLAY_NAME_PRIMARY + "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP
+ "=1";
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// load from the "Contacts table"
Uri contentUri = Contacts.CONTENT_URI;
// no sub-selection, no sort order, simply every row
// projection says we want just the _id and the name column
return new CursorLoader(getActivity(), contentUri, PROJECTION, null, null, SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Once cursor is loaded, give it to adapter
Map<String, Integer> allContacts = new HashMap<String, Integer>();
mAdapter.swapCursor(data);
List<String> contactList = new ArrayList<String>();
for (data.moveToFirst(); !data.isAfterLast(); data.moveToNext()) {
String contactId = data.getString(data.getColumnIndex(Contacts._ID));
String display = data.getString(data.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
contactList.add(display);
}
mAdapter.setContactList(contactList);
//mCallback.setInitialContactList(allContacts);
}
The reason I had to do above code is that to set checked on checkedTextView
, I think I need to call:
getListView().setItemChecked(positon, true);
However, I only know the position of the cursor, but not the position of the ListView
. Is there anyway to find the listview position based on cursor position? If so, I don't even need above getView()
code.
Also, should I override getView()
or bindView()
? The bindView does not have position so I cannot cal getListView().setItemChecked(position, true)
. Any suggestion for a different approach?
This seems to be a very common use case. But I have spent many days trying different things and it still does not work. Please help me. Thanks a lot!
Keine korrekte Lösung