【问题标题】:How to programmatically check/uncheck listview with SimpleCursorAdapter如何使用 SimpleCursorAdapter 以编程方式检查/取消选中列表视图
【发布时间】:2014-02-19 07:02:57
【问题描述】:

我已经搜索了这个主题,但找不到我需要的东西,所以我不得不在这里寻求帮助。 基本上,我有一个包含两个片段的布局。顶部片段显示联系人组,下部片段显示所有联系人(有或没有组)。 当用户选中某个群组时,应用会查找属于该群组的所有联系人,并在下方片段中选中或取消选中这些联系人。

当用户选中或取消选中组时,我需要更新联系人列表,不仅是查看(CheckedTextView),还需要更新数据(选中/取消选中)。问题是 getListView().setItemChecked(position, value) 只占据列表的位置。我有关于 Contact._ID 或 Contact.DisplayName 的信息,但不知道要更新列表的哪个位置。我也知道需要更新的项目的光标位置,但该位置值与列表的位置不同。因此,当一个组被选中/取消选中时,总是会选中/取消选中错误的联系人。

我已尝试覆盖自定义 SimpleCursorAdapter 中的 bindView(),以便我可以切换 CheckedTextView,但它不会在屏幕上更新。

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);            

    }
}

我可以看到 tx.setChecked(true) 被调用,但屏幕上受影响的行没有被检查。

我需要给 CheckedTextView 添加一个监听器吗?还是在调用 tv.setChecked 后我需要做更多的事情?

非常感谢。

【问题讨论】:

    标签: android checkbox simplecursoradapter checkedtextview


    【解决方案1】:

    我有以下经过测试的代码。它使用简单光标适配器并显示电话通讯录中的联系人姓名和号码以及复选框。滚动列表时保留选中/未选中状态的复选框。希望这可以帮助。关键是使用 View Holder 标记视图,并使用光标位置标记复选框。

    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;
    }      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 2012-12-09
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多