final String[] listContent = {"1", "2", "3", "4" ,"5","6", "7", "8", "9", "10" ,"11" };
ListView list;
//adapter
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, listContent));
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//on item click method
StringBuilder vota = new StringBuilder();
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 1; i < listContent.length; i++) {
Log.i("Test", listContent[i] + ": " + checked.get(i));
if(checked.get(i) == true)
{
count++;
if(count<=5)
{
vota.append(" "+listContent[i]);
Log.i("vert",vota.toString());
}
else
{
//it displays an alert dialogbox
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You can't select more than 5 items!")
.setCancelable(false)
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
checked.delete(i);
AlertDialog ert = builder.create();
ert.show();
}
}
}
}
Asked
Active
Viewed 1,188 times
1

arianit
- 107
- 9
-
you have already implemented the code for limiting the number of selections to 5, is there any problem in it? some supporting text with your code would have been helpful. – Urban Sep 20 '11 at 17:17
-
the problem is that the dialogbox is displayed after selecting 3 items ...and when u click close, it is displayed again 2 times. after closing the dialog box everytime u select an item the dialogbox appears again. – arianit Sep 20 '11 at 17:24
-
i think there are some problems at the logic of loop ...it isnt working well – arianit Sep 20 '11 at 17:25