微调器工作正常,点击它有一个下拉菜单,旁边有复选框。我的问题是,当我点击一个勾选的复选框时,但当我关闭微调器并再次打开它时,该盒子未被攻击。我希望盒子继续检查我该怎么办? 这是我的代码。
ArrayList<StateVO> listbtop = new ArrayList<>();
for (int i = 0; i < btoppings.length; i++) {
StateVO stateVO = new StateVO();
stateVO.setTitle(btoppings[i]);
stateVO.setSelected(false);
listbtop.add(stateVO);
}
MyAdapter myAdapter = new MyAdapter(Guest.this, 0, listbtop);
spinnerbtop.setAdapter(myAdapter);
这是我的适配器类。
public class MyAdapter extends ArrayAdapter<StateVO> {
private Context mContext;
private ArrayList<StateVO> listState;
private MyAdapter myAdapter;
private boolean isFromView = false;
public MyAdapter(Context context, int resource, List<StateVO> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<StateVO>) objects;
this.myAdapter = this;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item, null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
}
});
return convertView;
}
}
答案 0 :(得分:0)
您必须在对象“StateVO”中保持选中状态,您是否可以尝试使用此getCustomView实现?
public View getCustomView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item, null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listState.get((Integer) holder.mCheckBox.getTag()).setSelected(isChecked);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mCheckBox.setTag(position);
holder.mTextView.setText(listState.get(position).getTitle());
holder.mCheckBox.setChecked(listState.get(position).isSelected());
return convertView;
}