从自定义列表适配器ListActivity中删除项目时遇到问题

时间:2014-02-24 13:18:49

标签: android listview android-listview listactivity listadapter

我正在使用list活动来显示带有复选框的自定义列表适配器的数据。当用户选中带复选框的项目时,底部会出现一个按钮,然后单击该按钮以删除项目。当用户选择一个项目并删除时,列表中的数据正常工作。但是当用户选择多个项目时,只有一个项目被删除但第二个项目仍然被检查。任何人都可以指出可能出现的问题吗?我在这里发布自定义适配器的getView()方法,onCheckedChangeListener和删除项目的代码。请相应帮助。

<code>Custom List Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;
        if(view == null) 
        {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.gmail_list_layout, null);

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

            view.setTag(viewHolder);


        }
        else
        {
            viewHolder = (ViewHolder) view.getTag();
            Log.i("mustang", "Items have been removed though.");
        }

        MailMessage msg = emails.get(position);

        viewHolder.checkView.setChecked(false);
        viewHolder.checkView.setTag(position);
        viewHolder.checkView.setOnCheckedChangeListener(checkListener);

        viewHolder.fromView.setText(msg.getFromArray().get(0));
        viewHolder.subjectView.setText(msg.getSubject());
        viewHolder.timeView.setText(msg.getReceivedDate());

        if(!msg.getIsSeen())
        {
            viewHolder.rootLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
        }

        return view;

    }

    OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {

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

            Log.i("mustang", "Positions Array: " + checkedPositions.size());

            context.itemsSelected(checkedPositions);    
        }
    };

单击按钮时删除项目的代码:

private OnClickListener deleteItemsListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ArrayList<Integer> positions = emails.getCheckedPositions();
            for(int p : positions)
            {
                messges.remove(p);
            }
            emails.notifyDataSetChanged();
            emails.clearList();

            deleteItems.setText("");
            deleteItems.setVisibility(Button.INVISIBLE);
}
}
</code>

这里的电子邮件是自定义适配器,而邮件是适配器中使用的列表。请帮我解决这个问题。提前谢谢。

0 个答案:

没有答案