0

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

关于如何获得 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 的帮助下在 ListView 的滚动上保持 CheckBoxes 状态。问题是,我的活动中有一个删除按钮,只要至少有一个复选框被选中或没有,它就应该可见/消失。如果您有任何想法,请告诉我。

4

1 回答 1

0

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

您可以使用 hashMap 将列表项保存为 KEY 并将“已检查”状态保存为值。当用户选中该框时,将键值添加到映射中,当用户取消选中该框时,则从映射中删除键。

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

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
    }
 }
于 2013-03-05T12:18:33.680 回答