我有一個RelativeLayout,它允許遊戲玩家輸入個人詳細信息,如姓名並定義他們的角色。佈局是根據有多少玩家動態創建的(這在活動前面已經定義)。 這全部包含在for循環中以生成每個佈局。添加onClickListener爲在for循環內動態創建的按鈕
我想添加一個onclicklistener,以便每個玩家輸入他們的詳細信息時,他們的個人數據將被存儲。
我到目前爲止使用的代碼產生了一個ArrayIndexOutOfBoundsException。
你能解釋一下我應該如何糾正這段代碼,謝謝。
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]); }
一個問題可能是如何將字符串值從循環傳遞到onClick()函數。由於
,請複製粘貼登錄貓輸出也和你爲什麼不能使用列明的得分爲每個條目列表視圖? – Aneez
背後有什麼想法:'row_Number = new TextView [input1];'? – donfuxx
@Aneez,我沒有使用listvew,因爲我需要人們在這裏輸入信息,並將editview附加到列表視圖似乎沒有工作,當我最初嘗試它 – Daniel