user3182266
user3182266

Reputation: 1330

How to set the size of the array correctly?

I am having trouble setting the size of my array SelectedModes. I have a dialog check box and I want to send information about which boxes have been checked.

private boolean transportDialog()
{
    String[] typeOfTransport;

    typeOfTransport = new String[modes.length];     
    SelectedModes = new String[modes.length];
    final boolean[] itemsChecked = new boolean[modes.length];
    for (int i = 0; i < modes.length; i++) 
    {           
        typeOfTransport[i] = modes[i].Name; 

        if (modes[i].Selected == true) 
        {
            itemsChecked[i] = true;
        }           
    }

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Select your transport:");
    dialogBuilder.setMultiChoiceItems(typeOfTransport, itemsChecked, new DialogInterface.OnMultiChoiceClickListener()
    {           
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) 
        {               
            itemsChecked[which] = isChecked;                 
        }           
    });

    dialogBuilder.setPositiveButton("Set", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
            String selectetdVal = " ";              

            for (int i = 0; i < modes.length; i++) 
            {                  
               if (itemsChecked[i]) 
               {
                 SelectedModes[i] = modes[i].Code;
                 selectetdVal = selectetdVal + modes[i].Name + " ";     
                 itemsChecked[i]=false; 
               }
            }       

            //textBox2.setText(selectetdVal);
            try 
            {
                mXmlRpcClient.call(mSetModesFunc, mSessionID, SelectedModes);
            } catch (XMLRPCException e)             {

                e.printStackTrace();
            }    
            Toast.makeText(MainWindow.this, selectetdVal,Toast.LENGTH_SHORT).show();
        }

    });     

    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
    return true;
}

So I have an array with the type of the transport typeOfTransport[] and I set the size of it to be the size of how many types it gets from the server. Also with each type of transport there is a "Code" that i get from the server that is the code of the specific type of transport. So I want to show the dialog with the checkboxes to the user, allow him to select what he wants to check and put the "Code" of the transport in the array SelectedModes. But I get an error that I sent NULL values back to the XML server. I am guessing the problem is that the array is too big and I only put values in the positions that have been checked. So Can you tell me how to set the size of my array to be as big as the boxes that have been checked and to set the right "Code" value in the array.

Upvotes: 2

Views: 90

Answers (1)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

If you just need to send out WHICH boxed have been checked, you can use a bit mask.
It's an integer value where each checkbox is a digit in a binary number.
So, the first one is 1, the second one is 2, the third one is 4 and so on...

When you check or uncheck the box, you will set or unset its value.
So, if the first and the third checkboxes are checked, you'll add 1 (0001) and 4 (0100) and get 5 (0101). On the other side, you will receive this int and "decode" it.

See an example on using bitmasks in Java

Upvotes: 3

Related Questions