friends,
i want to limit checkbox selection in android listivew to for example only 3 checkboxes should be selected otherwise it should give error message.
u
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);
}
}
Take a static int count variable and increment it using these condition
holder.checkBox.isChecked() if it is true then increment in the count then check count>3 then show popup to user
I hope this is Help.
i solved the issue "after scroll listview user can also again select more than 3 check boxex" Thanks for you patience set 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());
}
And Most imprtant is that add count=0 in else and viewHolder.chkContact.setTag(position) after else;
if(convertView == null){
}else
{
viewHolder = (ViewHolder) convertView.getTag();
count=0;
}
viewHolder.chkContact.setTag(position);
`
finally i solved this issue :) where as globalInc is global variable with default value 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);
}
});
Try this:
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));
}
}
tried to improve my code with ViewHolder pattern but failed. if anyone have better idea, please let me know.