Question

I have a RelativeLayout that allows players of a game to input personal details such as name and define their role. The Layouts are created dynamically depending on how many players there are (This is defined earlier in the activity). This is all contained in a for loop to generate each layout.

I am trying to add an onclicklistener so that as each player enters their details their individual data will be stored.

The code I have used so far produces an ArrayIndexOutOfBoundsException.

Could you please explain how I should correct this code, thanks.

    public void buildDynamicViews(Integer input1) {
        int textId = 10; 
    //Arrays
    groupDetailsList = (RelativeLayout)findViewById(R.id.playersNameLayout);
    row_Number = new TextView[input1];
    spinnerArray = new Spinner[input1];
    groupDetailsContainer = new RelativeLayout[input1];
    firstNameText = new TextView[input1];
    lastNameText = new TextView[input1];
    fNameInput = new EditText[input1];
    lNameInput = new EditText[input1];
    playerDetailsButton = new Button[input1];


    //displays lines of text with instructions
    text = (TextView)findViewById(R.id.displayMessage);
    ViewGroup.LayoutParams params = text.getLayoutParams();
    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    text.setId(textId);
    ((MarginLayoutParams) params).setMargins(0,0,0,20);
    text.setLayoutParams(params);
    String txt1 = getResources().getString(R.string.player_details_Title);
    String txt2 = getResources().getString(R.string.players_details_message);
    String txt = txt1+"\n"+txt2;
    text.setText(txt);

     //Scrollview to contain relativeLayouts created in for loop
    ScrollView scroll = (ScrollView)findViewById(R.id.playerNameScroller);
    RelativeLayout.LayoutParams scrollerLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    scrollerLayoutParams.addRule(RelativeLayout.BELOW, text.getId());
    scroll.setLayoutParams(scrollerLayoutParams);


    //Loop
    for ( i = 0; i < input1; i++) {

    //generate relativelayout to house each individual players data input section
    groupDetailsContainer[i] = new RelativeLayout(this);
        groupDetailsContainer[i].setId(20+i);
        RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if(i==0){
                containerParams.addRule(RelativeLayout.BELOW ,text.getId());
                }
                else
            {
            containerParams.addRule(RelativeLayout.BELOW, (i+19));
            }
        containerParams.setMargins(0,0,0,20);
        groupDetailsContainer[i].setLayoutParams(containerParams);


        //generate left hand box for player number
        row_Number[i]= new TextView(this);
        LayoutParams rowNumParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rowNumParams.addRule(RelativeLayout.ALIGN_LEFT);
        if(i ==0)
            rowNumParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        else
        {
            rowNumParams.addRule(RelativeLayout.BELOW, (i-1));

        }
        row_Number[i].setLayoutParams(rowNumParams);
        row_Number[i].setText("Details for Player Number:" + (i+1));
        row_Number[i].setHeight(80);
        row_Number[i].setTextSize(20);
        row_Number[i].setWidth(125);
        row_Number[i].setId((i+1));

        //generate  box for text first name
        RelativeLayout.LayoutParams fNameParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        fNameParams.addRule(RelativeLayout.RIGHT_OF, row_Number[i].getId() );
        firstNameText[i] = new TextView(this);
        firstNameText[i].setTextSize(20);
        firstNameText[i].setHeight(40);
        firstNameText[i].setText("Enter First Name:");
        firstNameText[i].setId(31*(i+1));
        firstNameText[i].setLayoutParams(fNameParams);

        //generate  EditTextbox for first name input
    RelativeLayout.LayoutParams fNameinputParams = new LayoutParams(200, 25);
        fNameinputParams.addRule(RelativeLayout.RIGHT_OF, firstNameText[i].getId());
        fNameinputParams.addRule(RelativeLayout.ALIGN_BASELINE, firstNameText[i].getId());
        fNameinputParams.setMargins(10, 0, 10, 0);
        fNameInput[i] = new EditText(this);
        fNameInput[i].setLayoutParams(fNameinputParams);
        fNameInput[i].setId(32*(i+1));
        fNameInput[i].setTextSize(20);

         //generate  button
   RelativeLayout.LayoutParams detailsButtonParams = new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        playerDetailsButton[i] = new Button(this);
        detailsButtonParams.addRule(RelativeLayout.RIGHT_OF, spinnerArray[i].getId());
        detailsButtonParams.addRule(RelativeLayout.ALIGN_BOTTOM, spinnerArray[i].getId());
        detailsButtonParams.setMargins(20, 0, 20, 0);
        playerDetailsButton[i].setLayoutParams(detailsButtonParams);
        playerDetailsButton[i].setWidth(250);
        playerDetailsButton[i].setHeight(20);
        playerDetailsButton[i].setText("Press to save details");

    //generate  OnClick Listener for button
        PlayerDetailsButtonOnClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String FName = firstNameText[i].toString();
                String LName = lastNameText[i].toString();
                String fullName = FName+" "+LName;
                toast(fullName);

                }
            };


        playerDetailsButton[i].setOnClickListener(PlayerDetailsButtonOnClick);

        //wrap each view to the layout        
    groupDetailsContainer[i].addView(row_Number[i]);
        groupDetailsContainer[i].addView(fNameInput[i]);
        groupDetailsContainer[i].addView(lNameInput[i]);
        groupDetailsContainer[i].addView(firstNameText[i]);
        groupDetailsContainer[i].addView(lastNameText[i]);
        groupDetailsContainer[i].addView(spinnerArray[i]); 
        groupDetailsContainer[i].addView(playerDetailsButton[i]);
        groupDetailsList.addView(groupDetailsContainer[i]);   }

One issue may also be how to pass the string values from the loop into the onClick() function. Thanks

Was it helpful?

Solution

The pojo class is a simple class (you can add the data trough the setters or make a simple constructor):

    private class POJOName {
    String firstName;
    String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

this class can be used to store the first name and also the last name and then you use playerDetailsButton[i].setTag( here you put the pojo object ); and then

//generate  OnClick Listener for button
    PlayerDetailsButtonOnClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            POJOName pojo = (POJOName)v.getTag();
            String fullName = pojo.getFirstName()+" "+pojo.getLastName();
            toast(fullName);

            }
        };

OTHER TIPS

I also managed to solve this a second way as well.

I assigned a final int j variable to the int i value within the for loop.

Then in the OnClick() I referenced the relevant editview like this [class].this.[viewname][j].gettext().toString(); So my code looks like this:

        final int j = i;


        playerDetailsButton[i].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("detailsOnClick", "Button Pressed");
                inputFName = GroupDetails1.this.fNameInput[j].getText().toString();
                Log.d("inputFNameOnClick", inputFName);

                inputLName = (GroupDetails1.this.lNameInput[j].getText()).toString();
                toast(inputFName);
                toast(inputLName);
    }

Thanks for all your help and input

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top