嘿,我想从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
适配器没有刷新并且没有显示提示
答案 0 :(得分:3)
我只是自己修理
我扔掉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;
}
答案 1 :(得分:0)
如果使用以下类,则可以按照以下方法添加提示
public class MultiSelectionSpinner extends android.support.v7.widget.AppCompatSpinner implements
DialogInterface.OnMultiChoiceClickListener {
String _title;
String[] _items = null;
boolean[] mSelection = null;
ArrayAdapter<String> simple_adapter;
List<KotItems> mKotdata;
int pos;
public MultiSelectionSpinner(Context context)
{
super(context);
simple_adapter = new ArrayAdapter<String>(context,
R.layout.spinneritemstyle);
super.setAdapter(simple_adapter);
}
public MultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
simple_adapter = new ArrayAdapter<String>(context,
R.layout.spinneritemstyle);
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 {
simple_adapter.add("N/A");
throw new IllegalArgumentException(
"Argument 'which' is out of bounds.");
}
}
@Override
public boolean performClick() {
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(_items, mSelection, this);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1)
{
mKotdata.get(pos).setKotnote(buildSelectedItemString());
}
});
builder.show();
return true;
}
@Override
public void setAdapter(SpinnerAdapter adapter) {
throw new RuntimeException(
"setAdapter is not supported by MultiSelectSpinner.");
}
//Add Your Hint in this methord
public void setItems(List<String> items) {
if(items.size()>0){
_items = items.toArray(new String[items.size()]);
mSelection = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add("Add Your Hint Here");
Arrays.fill(mSelection, false);
}
}
public void setArrayList(List<KotItems> mKotdata,int pos){
this.mKotdata = mKotdata;
this.pos = pos;
}
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());
}
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]);
}
}
System.out.println("u8u8 "+sb.toString());
return sb.toString();
}
}