1
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();

           }

             }
    }

}
4

0 回答 0