问题
I have an ArrayList of Strings defined called names defined and I add to my ListView object as follows:
I create an Array Adapter and set multiple choice mode in both the Array Adapter and ListView object. This works great, I can scroll and pick multiple objects. But the problem is I want to force the user into being able to pick only 4 items from the ListView. How can I set a restriction?
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,names);
nameList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
nameList.setAdapter(adapter);
回答1:
You could create a List
and whenever the List
exceeds a certain size you could uncheck the first option in the List
and delete it from the List
then add the new one or even stop them from selecting another.
So just check in you onClick
what the size of the list is then decide whether or not you want to process that additionally clicked item.
回答2:
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.
回答3:
Yes, I had an idea and found a good solution.
I solved the issue "after scroll listview user can also again select more than 3 checkboxes". 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);
来源:https://stackoverflow.com/questions/24497827/set-limit-on-listview-multiple-section-mode