使用包含复选框的自定义listadapter从ListActivity中删除项目

时间:2014-02-21 18:44:31

标签: android listview checkbox listactivity

我正在尝试从listactivity中删除包含自定义列表适配器的列表项。自定义列表适配器具有文本视图和复选框。当用户通过单击复选框选择项目时,屏幕底部会出现一个按钮,然后单击按钮,我想从适配器中删除项目。我已经通过将位置设置为复选框的标签来实现解决方案,但它不起作用。检查的位置和列表视图索引未正确映射。我在这里发布我的代码:

ListAdapter

<code>

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if(view == null) 
    {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.gmail_list_layout, null);

        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.checkView = (CheckBox) view.findViewById(R.id.check);
        viewHolder.timeView = (TextView) view.findViewById(R.id.time_content);
        viewHolder.fromView = (TextView) view.findViewById(R.id.from_content);
        viewHolder.subjectView = (TextView) view.findViewById(R.id.subject_content);
        viewHolder.rootLayout = (ViewGroup) view.findViewById(R.id.rootGmail);

        Log.i("mustang", "Adding view at Position: " + position);
        viewHolder.checkView.setTag(position);
        viewHolder.checkView.setOnCheckedChangeListener(checkListener);

        view.setTag(viewHolder);    
    }


OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Integer position = (Integer)buttonView.getTag();
            Log.i("mustang", "Position: " + position);
            if(isChecked)
            {
                checkedPositions.put(position, position);
                buttons.put(position, buttonView);
            }
            else
            {
                checkedPositions.remove(position);
                buttons.remove(position);
            }

            Log.i("mustang", "Positions Array: " + checkedPositions.size());
            ArrayList<Integer> positions = new ArrayList<Integer>(checkedPositions.values());
            context.itemsSelected(positions);
        }
    };



     public ArrayList<Integer> getCheckedPositions()
     {
return  new ArrayList<Integer>(checkedPositions.values());
        }

</code>

每当选中复选框时,底部会显示按钮以删除项目。显示代码和删除代码的按钮如下所示:

当复选框选中的项目多于一个时显示按钮:

<code>
public void itemsSelected(ArrayList<Integer> positions)
    {
        Log.i("mustang", "positions.size(): " + positions.size());
        deleteItemsButton.setText("Delete " + positions.size() + " mails?");
        deleteItemsButton.setVisibility(positions.size() > 0 ? Button.VISIBLE : Button.INVISIBLE);
    }

</code>

当用户在代码运行后点击它时:

<code>
private OnClickListener deleteItemsListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ArrayList<Integer> positions = emails.getCheckedPositions();

            for(Integer p : positions)
            {
                emails.remove(emails.getItem(p.intValue()));
            }


            emails.notifyDataSetChanged();
        }
    };
</code>

请指出我正在以正确的方式实施该技术。

提前致谢。

0 个答案:

没有答案