java - 带提示的多选微调器

标签 java android

嘿,我想从 https://github.com/wongk/MultiSelectSpinner 添加提示到 MultiSelector 我把它与 https://github.com/ravivyas84/AndroidSpinnerHint 混合在一起

它看起来像这样我添加自己的 SpinnerAdapterProxy延伸ArrayAdapter<String>

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

    _proxyAdapter = new SpinnerAdapterProxy(context, android.R.layout.simple_spinner_item);
    super.setAdapter(_proxyAdapter);
    _context = context;
}
(...)

public void setHint(String hint) {
    _proxyAdapter.setHint(hint);
}

适配器

public class SpinnerAdapterProxy extends ArrayAdapter<String> {

private LayoutInflater mInflator;
private TextView text;
private boolean selected = false;
private String hint;

public SpinnerAdapterProxy(Context context, int resource) {
    super(context, resource);
    // TODO Auto-generated constructor stub
    mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
                convertView = mInflator.inflate(android.R.layout.simple_spinner_item, null);
        }
        text = (TextView) convertView.findViewById(android.R.id.text1);
        if (!selected) {
                text.setHint(hint);
        } else {
                text.setHint(this.getItem(position));
        }
        return convertView;
}

public void setHint(String hint) {

    this.hint = hint;
    this.notifyDataSetChanged();
    this.notifyDataSetInvalidated();
}

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

以及我创建的地方 MultiSelectSpinnersetTitle

province = (MultiSelectSpinner) findViewById(R.id.subregion);
province.setHint(getResources().getString(R.string.choose_province));
province.setItems(getResources().getStringArray(R.array.provinces));

问题是setHint之后适配器未刷新且未显示提示

最佳答案

我自己修好了

我扔掉了SpinnerAdapterProxy并只修改了MultiSelectSpinner 通过改变

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

        if (!isAnySelect()) 
            select = _title;
        else 
            select = buildSelectedItemString();

        _proxyAdapter.clear();
        _proxyAdapter.add(select);
        setSelection(0);
    }
    else {
        throw new IllegalArgumentException("Argument 'which' is out of bounds.");
    }
}

public void setTitle(String title) {
    _title = title;
    _proxyAdapter.clear();
    _proxyAdapter.add(title);
    setSelection(0);
}

并添加了测试功能,测试是否选择了任何项目

private boolean isAnySelect() {
    for (boolean b : _selection) {
        if (b == true) return true;
    }
    return false;
}

关于java - 带提示的多选微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349839/

相关文章:

java - 如何从 Java 中的 tiff 检索 16 位像素数据?

java - 使用mockito和spy进行单元测试会导致错误

android - SetPage(Xamarin.Forms.Page)' 已过时错误 - Android

android - “Unfortunately My Android App has stopped”

java - 适用于 Windows 和 Macos 的 Eclipse RCP SWT 菜单

java - 运行时发生log4j.PropertyConfigurator错误说找不到适合configure(FileInputStream)的方法

android - 在 Flutter 中改变方向时保留 TextField 值

java - 如何将 String[] 字符串转换为单独的字符串?

java - 对方付费电话要 map 吗?

android - 查看特定文件夹中的音频