嗨我有一個列表視圖中的複選框 我有八個選擇,我希望用戶能夠只檢查四個,然後鎖定複選框,除非他取消選中。如何確定可以在Android中檢查的複選框的數量?
這是我公司主營:
public class Ch2 extends Activity implements OnItemClickListener{
ListView lv;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
int num;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_ch2);
lv = (ListView) findViewById(R.id.listView2);
adapter = new cusCh2(this,getModel());
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
final CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked){
Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_LONG);
// disable checkbox
}
}
});
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if(checkbox.isChecked()) {
num++;
if (num == 4) {
checkbox.setEnabled(false);
}
return "is checked";
} else
return "is not checked";
}
private List<Model> getModel() {
list.add(new Model("a"));
list.add(new Model("b"));
list.add(new Model("c"));
list.add(new Model("d"));
list.add(new Model("e"));
list.add(new Model("f"));
list.add(new Model("m"));
list.add(new Model("o"));
return list;
}
,這我ArrayAdapter
public class cusCh2 extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public cusCh2(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
final ViewHolder finalViewHolder = viewHolder;
final ViewHolder finalViewHolder1 = viewHolder;
final ViewHolder finalViewHolder2 = viewHolder;
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
,這我的模型
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
當然
我row.xml
文件
按我說的,我想要讓我的用戶最多可以查看四個框。
的解決方案,我試過:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
if(!isChecked){
num++;
if(num>3){
finalViewHolder.checkbox.setChecked(false);}
if(!isChecked){
num--;
if(num<3){
finalViewHolder.checkbox.setChecked(true);}
}
}
}
});
它的工作爲停止檢查,但是當我滾動列表,我失去了什麼,我已經選中。
編輯2:
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
Toast toast= Toast.makeText(getContext(), String.valueOf(getPosition), Toast.LENGTH_SHORT);
if (isChecked) {
num++;
l.add(String.valueOf(getPosition));
if(l.size() > 3){
finalViewHolder.checkbox.setChecked(false);
}
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
else if(!isChecked){
finalViewHolder.checkbox.setChecked(true);
}
}
});
還當我滾動我不知所措,我已經檢查
編輯2:
viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(finalViewHolder.checkbox.isChecked()); // Set the value of checkbox to maintain its state.
Toast toast= Toast.makeText(getContext(), String.valueOf(getPosition), Toast.LENGTH_SHORT);
if (finalViewHolder.checkbox.isChecked()) {
l.add(String.valueOf(getPosition));
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
if (l.size() > 4) {
finalViewHolder.checkbox.setChecked(false);
}
if (!finalViewHolder.checkbox.isChecked()) {
finalViewHolder.checkbox.setChecked(true);
}
}
}
}
);
我希望有人能幫助我,謝謝。
以及本作另一個問題 鎖後,如果嘗試校驗,那麼然後滾動背框將被選中自動 – chaos
CNü分享我們的代碼...ü必須失去somethng ..我想再次調用你的活動的創建者..如果你鎖定或解鎖 – Meenal
我編輯我的問題,並添加我的代碼,工作正常,但一個問題,當鎖定並嘗試檢查一個是的,我不能但當滾動和回到一個我試過它檢查自動 – chaos