添加一个按钮以选择Multiselect微调器中的所有项目

时间:2014-04-11 19:51:09

标签: android

早上好,我有一个Android应用程序,我添加了一个MultiSelectSpinner,我发现并编辑了一点,现在我想添加一个按钮来选择所有项目,我搜索了很多,我没有找到方法,所有我可以认为是非常复杂的。 有人必须实现类似的东西吗?我很感激帮助。 祝你有愉快的一天。

以下是我的MultiSelectSpinner的代码:

public class MultiSelectionSpinner extends Spinner implements
    OnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;

ArrayAdapter<String> simple_adapter;

public MultiSelectionSpinner(Context context) {
    super(context);

    simple_adapter = new ArrayAdapter<String>(context,
            android.R.layout.simple_spinner_item);
    super.setAdapter(simple_adapter);
}

public MultiSelectionSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);

    simple_adapter = new ArrayAdapter<String>(context,
            android.R.layout.simple_spinner_item);
    super.setAdapter(simple_adapter);
}

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (mSelection != null && which < mSelection.length) {
        mSelection[which] = isChecked;

        simple_adapter.clear();
        simple_adapter.add(buildSelectedItemString());
    } else {
        throw new IllegalArgumentException(
                "Argument 'which' is out of bounds.");
    }
}

@Override
public boolean performClick() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMultiChoiceItems(_items, mSelection, this);
    builder.show();
    return true;
}

@Override
public void setAdapter(SpinnerAdapter adapter) {
    throw new RuntimeException(
            "setAdapter is not supported by MultiSelectSpinner.");
}

public void setItems(String[] items) {
    _items = items;
    mSelection = new boolean[_items.length];
    simple_adapter.clear();
    simple_adapter.add("Ningun sector seleccionado");
    Arrays.fill(mSelection, false);
}

public void setItems(List<String> items) {
    _items = items.toArray(new String[items.size()]);
    mSelection = new boolean[_items.length];
    simple_adapter.clear();
    simple_adapter.add(_items[0]);
    Arrays.fill(mSelection, false);
}

public void setSelection(String[] selection) {
    for (String cell : selection) {
        for (int j = 0; j < _items.length; ++j) {
            if (_items[j].equals(cell)) {
                mSelection[j] = true;
            }
        }
    }
}

public void setSelection(List<String> selection) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
    }
    for (String sel : selection) {
        for (int j = 0; j < _items.length; ++j) {
            if (_items[j].equals(sel)) {
                mSelection[j] = true;
            }
        }
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int index) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
    }
    if (index >= 0 && index < mSelection.length) {
        mSelection[index] = true;
    } else {
        throw new IllegalArgumentException("Index " + index
                + " is out of bounds.");
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int[] selectedIndicies) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
    }
    for (int index : selectedIndicies) {
        if (index >= 0 && index < mSelection.length) {
            mSelection[index] = true;
        } else {
            throw new IllegalArgumentException("Index " + index
                    + " is out of bounds.");
        }
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public List<String> getSelectedStrings() {
    List<String> selection = new LinkedList<String>();
    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            selection.add(_items[i]);
        }
    }
    return selection;
}

public List<Integer> getSelectedIndicies() {
    List<Integer> selection = new LinkedList<Integer>();
    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            selection.add(i);
        }
    }
    return selection;
}

public boolean haveSelectedItems() {
    return (mSelection != null && mSelection.length > 0);
}

public boolean haveItems() {
    return (_items != null && _items.length > 0);
}

private String buildSelectedItemString() {
    StringBuilder sb = new StringBuilder();
    boolean foundOne = false;
    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            if (foundOne) {
                sb.append(", ");
            }
            foundOne = true;
            sb.append(_items[i]);
        }
    }
    return sb.toString();
}
}

由于

1 个答案:

答案 0 :(得分:2)

使用以下代码(更改performeClick方法并添加此selectAll方法)以在您的应用上启用全选。 我添加了两个按钮(setNegativeButton,setPositiveButton)及其文本(全部清除,全选),并且我不想在用户单击这些按钮后立即关闭对话框。请享用!

@Override
public boolean performClick() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMultiChoiceItems(_items, mSelection, this);
    builder.setTitle(title);
    builder.setNegativeButton("Clear All"),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {}
            }).setPositiveButton("Select All"),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {}
            });

    final AlertDialog dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Boolean wantToCloseDialog = false;
                    selectAll(false, dialog);

                    if (wantToCloseDialog)
                        dialog.dismiss();
                }
            });
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Boolean wantToCloseDialog = false;
                    selectAll(true, dialog);

                    if (wantToCloseDialog)
                        dialog.dismiss();
                }
            });

    return true;
}

protected void selectAll(boolean isSelectAll, AlertDialog dialog) {
    if (mSelection != null) {
        for (int i = 0; i < _items.length; i++) {
            mSelection[i] = isSelectAll;
            ((AlertDialog) dialog).getListView().setItemChecked(i, isSelectAll);

        }

        simple_adapter.clear();
        simple_adapter.add(buildSelectedItemString());
    } else {
        throw new IllegalArgumentException("mSelection is null");
    }
}
相关问题