朋友,
我想限制android listivew中的复选框选择,例如,只应选择3个复选框,否则应该给出错误信息。
用户可以从列表中选择任意三个复选框 任何人都指导我如何实现这一目标?这是我的适配器
public class AdapterContacts extends BaseAdapter {
private LayoutInflater mInflater;
public Context context;
public static List<myContacts> contacts;
public AdapterContacts(Context context,List<myContacts> list) {
mInflater = LayoutInflater.from(context);
this.context = context;
contacts= list;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_contacts, null);
holder = new ViewHolder();
holder.contactName = (TextView) convertView.findViewById(R.id.contactName);
holder.contactNumber = (TextView) convertView.findViewById(R.id.contactNumber);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
myContacts contact = getItem(position);
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {
myContacts c = (myContacts) checkboxView.getTag();
c.setSelected(isChecked);
// to put that check of selection limit with error
}
});
holder.checkBox.setTag(contact);
holder.checkBox.setChecked(contact.isSelected());
holder.contactName.setText(contact.getContactName());
holder.contactNumber.setText(contact.getPhoneNumber());
return convertView;
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public myContacts getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int arg0) {
return 0;
}
class ViewHolder {
TextView contactName;
TextView contactNumber;
CheckBox checkBox;
}
}
任何帮助将不胜感激。
答案 0 :(得分:8)
最后我解决了这个问题:)其中globalInc是全局变量,默认值为0
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked)
{
myContacts c = (myContacts) checkboxView.getTag();
if(isChecked)
{
globalInc++;
}
else if(!isChecked)
{
globalInc--;
}
if(globalInc >= 4)// it will allow 3 checkboxes only
{
Toast.makeText(context, "Error = " + globalInc, Toast.LENGTH_LONG).show();
checkboxView.setChecked(false);
globalInc--;
}
else
{
c.setSelected(isChecked);
}
System.out.println(" --------------- "+globalInc);
}
});
答案 1 :(得分:1)
获取一个静态int计数变量并使用这些条件递增它
holder.checkBox.isChecked()如果为true则递增计数 然后检查计数&gt; 3然后向用户显示弹出窗口
我希望这是帮助。
答案 2 :(得分:0)
int globalInc = 0;
boolean isCheckGlobal = false;
ArrayList<Integer> checkings = new ArrayList<Integer>();
@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.list_inner, null);
viewHolder = new ViewHolder();
viewHolder.friendName = (TextView) convertView
.findViewById(R.id.friendName);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.checkbox);
final MyAdapterSecond objectMyAdapter = new MyAdapterSecond();
viewHolder.checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int getPositionInClick = (Integer) v.getTag();
if (isCheckGlobal == false) {
if (checkings.contains(getPositionInClick)) {
globalInc--;
checkings.remove(checkings.indexOf(getPositionInClick));
} else {
objectMyAdapter.getCheck(false);
}
} else if (isCheckGlobal == true) {
if (checkings.size() < 4) {
globalInc++;
}
}
}
});
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(
buttonView.isChecked());
callCheck(buttonView, "hi1");
if (checkings.size() > 4 {
Toast.makeText(context, "Error = " + globalInc,
Toast.LENGTH_LONG).show();
objectMyAdapter.setCheck(buttonView);
isCheckGlobal = false;
} else {
buttonView.setSelected(isChecked);
if (isChecked
&& (!checkings.contains(getPosition))) {
checkings.add(getPosition);
}
else if(!isChecked){
if(checkings.contains(getPosition)){
checkings.remove(checkings.indexOf(getPosition));
}
}
isCheckGlobal = true;
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.friendName, viewHolder.friendName);
convertView.setTag(R.id.checkbox, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.friendName.setText(list.get(position).getname());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
class MyAdapterSecond {
CompoundButton buttonView;
public void setCheck(CompoundButton buttonView) {
this.buttonView = buttonView;
}
public void getCheck(boolean flag) {
this.buttonView.setChecked(flag);
}
}
答案 3 :(得分:0)
试试这个:
public class AccountListAdapter extends BaseAdapter {
@SuppressWarnings("unused")
private final static String TAG = AccountListAdapter.class.getSimpleName();
private Context context;
private List<Account> rowItems;
private int selectedItemCounter = 0;
private final int limit;
public AccountListAdapter(Context context, List<Account> items, int limit) {
this.context = context;
this.rowItems = items;
this.limit = limit;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final Account rowItem = (Account) getItem(position);
convertView = mInflater.inflate(R.layout.account_selection_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.textView);
ToggleButton tb = (ToggleButton) convertView
.findViewById(R.id.toggleButton);
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked && !rowItem.isSelected()) {
if (selectedItemCounter >= limit) {
Toast.makeText(context,
"can't be more" + selectedItemCounter,
Toast.LENGTH_SHORT).show();
buttonView.setChecked(false);
return;
}
rowItem.setSelected(true);
selectedItemCounter++;
} else if (!isChecked && rowItem.isSelected()) {
rowItem.setSelected(false);
selectedItemCounter--;
}
}
});
tv.setText(rowItem.getDevId());
tb.setChecked(rowItem.isSelected());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
尝试使用ViewHolder模式改进我的代码但失败了。 如果有人有更好的想法,请告诉我。
答案 4 :(得分:0)
我解决了“滚动listview用户之后还可以再次选择3个以上的boxex”的问题,谢谢您的耐心配合 设置static int count = 0;
if (isChecked) {
count++;
} else if (!isChecked) {
count--;
}
if (count >= 4) {
buttonView.setChecked(false);
count--;
} else {
int getPosition = (Integer) buttonView.getTag();
contact.get(getPosition).setSelected(buttonView.isChecked());
}
最重要的是,在else中添加count = 0,在else后面添加viewHolder.chkContact.setTag(position);
if(convertView == null){
}else
{
viewHolder = (ViewHolder) convertView.getTag();
count=0;
}
viewHolder.chkContact.setTag(position);
`