Question

I have searched this topic and could not find what I need, so I have to ask for help here. Basically, I have a layout with two fragments. The top fragment shows contact groups and the lower fragment shows all contacts (with group or without). When a user check a group, the app finds all contacts that belong to that group and check or uncheck those contacts in the lower fragment.

When the user check or uncheck the group, I need to update the contact list, not just view (CheckedTextView) but also the data (checked/unchecked). The problem is that getListView().setItemChecked(position, value) only takes position of the list. I have information about the Contact._ID or Contact.DisplayName, but do not know which position of the list to update. I also know the position of the cursor for the item that needs to be updated but that postion value is different from the positon of the list. As a result, when a group is checked/unchecked, always, the wrong contact is checked/unchecked.

I have tried override the bindView() in the customized SimpleCursorAdapter, so that I can toggle the CheckedTextView, but it does not update on the screen.

public void bindView(View view, Context context, Cursor cursor) { 
    String displayName = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
    CheckedTextView tv = (CheckedTextView) view.findViewById(android.R.id.text1);     
    tv.setText(displayName);
    if (selectedGroup != null && selectedGroup.containsKey(displayName)) {            
        tv.setChecked(true);            

    }
}

I can see that the tx.setChecked(true) gets called, but the affected row on screen is not checked.

Do I need to add a listener to the CheckedTextView? Or do I have to do more after call tv.setChecked?

Thanks a lot.

No correct solution

OTHER TIPS

I have following tested code. It uses Simple Cursor Adapter and displays Contact names and numbers from Phone Address book along with the check box . The check box which preserves the checked/unchecked state while list is scrolled. Hope this helps. Key is to tag the view with a View Holder and checkedbox with cursor position.

public void bindView(View view, Context context, final Cursor cursor) {

    ViewHolder holder = (ViewHolder) view.getTag();
    final String displayName = cursor.getString(cursor
            .getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
    final String phoneNumber = cursor
            .getString(cursor
                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
    final int cursorposition = cursor.getPosition();

    // TODO Auto-generated method stub
    if (holder == null) {
        /*
         * Creates a ViewHolder and store references to the child views we
         * want to bind data to.
         */
        holder = new ViewHolder();
        holder.displayName = (TextView) view.findViewById(R.id.displayname);
        holder.phoneNumber = (TextView) view.findViewById(R.id.phonenumber);
        holder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox01);
        holder.checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Save the position of the checked item for deletion later
                ContactItem itemTag = (ContactItem) v.getTag();
                Integer position = (Integer) itemTag.getCursorposition();// v.getTag();
                if (checkedItems.contains(position)) {
                    checkedItems.remove(position);
                    // ((View)v.getParent()).setBackgroundResource(0);
                    if (map.containsKey(position)) {
                        map.remove(position);
                    }

                } else {
                    checkedItems.add(position);
                    /*
                     * ContactItem contactItem = new ContactItem();
                     * contactItem.setName(displayName);
                     * contactItem.setNumber(phoneNumber);
                     * map.put(cursorposition,contactItem);
                     */
                    map.put(position, itemTag);
                    // ((View)v.getParent()).setBackgroundResource(R.drawable.messages_background_selected);
                }

            }
        });

        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.displayName.setText(displayName);
    holder.phoneNumber.setText(phoneNumber);
    holder.checkbox.setChecked(checkedItems.contains(cursorposition));

    ContactItem itemTag = new ContactItem();
    itemTag.setCursorposition(cursorposition);
    itemTag.setDisplayName(displayName);
    itemTag.setNumber(phoneNumber);
    holder.checkbox.setTag(itemTag);
    // holder.checkbox.setTag(cursorposition);
}

public Object[] getItems() {

    return map.values().toArray();
}

/**
 * Keeps references to child views to avoid unnecessary calls to
 * findViewById() on each row.
 */
private static class ViewHolder {
    private TextView displayName;
    private TextView phoneNumber;
    private CheckBox checkbox;
}      
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top