选中复选框上的ListView删除按钮

时间:2013-03-05 10:21:51

标签: android listview checkbox adapter

我需要以下流程来解决问题: 点击我的任何列表视图项目复选框后,会出现一个删除按钮。如果取消选中所有复选框,则删除按钮应消失。 我的问题是,一旦使一个setOnCheckedChangeListener使删除可见/消失,在我的CheckBox视图中,它会在我滚动列表视图后自动取消选中,并且在我的屏幕上不再可见(因为回收视图的原因)

有关如何获取total_number_of_checkboxes_checked变量的任何想法,该变量将以1增加或减少。(我重复一遍,计算所有选中的复选框,而不仅仅是屏幕上可见的复选框。)

编辑:添加代码


适配器


public class AdminNotesAdapter extends BaseAdapter {

private ArrayList<AdminNoteShortenHolder> mNotesList = new ArrayList<AdminNoteShortenHolder>();

private AdminNoteShortenHolder mNoteHolder = new AdminNoteShortenHolder();
private ViewHolder holder = null;

private Activity activity;

public AdminNotesAdapter(ArrayList<AdminNoteShortenHolder> array,
        Activity activity) {
    this.activity = activity;
    this.mNotesList = array;

}

@Override
public int getCount() {
    return mNotesList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    mNoteHolder = mNotesList.get(position);

    if (convertView == null) {
        convertView = activity.getLayoutInflater().inflate(
                R.layout.row_adminnotes, null);
        holder = new ViewHolder(convertView);
        holder.cb_row_adminNotesCheck
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        int getPosition = (Integer) buttonView.getTag();
                        mNotesList.get(getPosition).setSelected(isChecked);

                    }
                });
        convertView.setTag(holder);
        convertView.setTag(R.id.cb_row_adminnotesCheck,
                holder.cb_row_adminNotesCheck);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.img_row_adminnotesNew
            .setVisibility(0 == mNoteHolder.note_read ? View.VISIBLE
                    : View.GONE);

    holder.txt_row_adminnotesNoteSubject.setText(mNoteHolder.note_subject);
    holder.txt_row_adminnotesNoteUser.setText("from "
            + mNoteHolder.note_from_user + " | " + mNoteHolder.note_time);
    holder.txt_row_adminnotesNotePreview.setText(mNoteHolder.note_content);

    holder.cb_row_adminNotesCheck.setTag(position);
    holder.cb_row_adminNotesCheck.setChecked(mNotesList.get(position)
            .isSelected());

    return convertView;
}

static class ViewHolder {

    public ImageView img_row_adminnotesNew;
    public TextView txt_row_adminnotesNoteSubject;
    public TextView txt_row_adminnotesNoteUser;
    public TextView txt_row_adminnotesNotePreview;
    public CheckBox cb_row_adminNotesCheck;

    public ViewHolder(View convertView) {

        img_row_adminnotesNew = (ImageView) convertView
                .findViewById(R.id.img_row_adminnotesNew);

        txt_row_adminnotesNoteSubject = (TextView) convertView
                .findViewById(R.id.txt_row_adminnotesNoteSubject);
        txt_row_adminnotesNoteUser = (TextView) convertView
                .findViewById(R.id.txt_row_adminnotesNoteUser);
        txt_row_adminnotesNotePreview = (TextView) convertView
                .findViewById(R.id.txt_row_adminnotesNotePreview);
        cb_row_adminNotesCheck = (CheckBox) convertView
                .findViewById(R.id.cb_row_adminnotesCheck);

    }

}
}

AdminNoteShortenHolder(模型类)

public class AdminNoteShortenHolder extends BaseHolder {

public int note_id;
public String note_from_user;
public String note_subject;
public String note_content;
public String note_time;
public int note_read;

private boolean selected;

@Override
public void parse(JSONObject jsonObject) {

    note_id = jsonObject.optInt("note_id", -1);
    note_from_user = jsonObject.optString("note_from_user");
    note_subject = jsonObject.optString("note_subject");
    note_content = jsonObject.optString("note_content");
    note_time = jsonObject.optString("note_time");
    note_read = jsonObject.optInt("note_read", -1);
}

public boolean isSelected() {

    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}
}

这是我到目前为止所做的,所以我设法在setTag / getTag的帮助下维护CheckBoxes状态而不是滚动ListView。问题是,我的Activity中有一个删除按钮,只要选中至少一个复选框或没有复选框,就应该可见/删除。 如果您有任何想法,请告诉我。

1 个答案:

答案 0 :(得分:0)

您应该维护/保留“已检查”复选框的状态,以便在向上和向下滚动时检查它,因为在向上/向下滚动列表视图时,列表适配器会重新创建每个列表项。

您可以使用hashMap将list-item保存为s KEY,将'Checked'状态保存为值。 当用户选中标记框时,将键值添加到地图中,当用户取消选中该框而不是从地图中删除键时。

在渲染列表视图中使用此地图。代码如下:

private Map<Integer, Boolean> map =  new HashMap<Integer, Boolean>();    

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

     Boolean present = map.get(position);
     if(present != null && present.booleanValue()){
       // set checkbox checked
     } else {
       // set checkbox un-cheked
     }

     holder.cb_row_adminNotesCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();
                    mNotesList.get(getPosition).setSelected(isChecked);
                    if(isChecked){
                       map.put(position, true);
                    } else {
                       map.remove(position);
                    }
                    setButtonVisibility();
                }
     });
}

 private void setButtonVisibility()
 {
    if(map.size() > 0){
       // set button visible
    } else {
       // set button invisible
    }
 }