嘿,我想从 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;
}
}
以及我创建的地方 MultiSelectSpinner
和setTitle
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/