更新按钮的 arrayIndex onClick 导致问题
我正在实现一个测验,这里有一个按钮的方法,因为
public void playquiz(final int arrayIndex) {
setContentView(R.layout.quiz);
next = (Button) findViewById(R.id.nextBtn);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) {
int totalpoints = correctAnswerCount*10;
Intent scoreintent = new Intent(TriviaQuiz.this,ScoreBoard.class);
startActivity(scoreintent);
}
else
{
playquiz(arrayIndex+1);
}
}
我想做的是,在该方法中我加载另一个布局并为该布局中的按钮分配一个 onclick。
现在我的问题是,我最初得到的 arrayIndex,我必须在单击下一个按钮时更新它,并且基于此我还有一些其他条件需要检查。
但如果我确实喜欢 playquiz(arrayIndex+1);,它会要求我将 arrayIndex 声明为 Final,这是为什么?
即使这样,它的行为也没有按照预期的那样进行。
onClick 内的 if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) 没有发生 有
什么建议吗?
I am implementing a quiz and here am having a method for my button as
public void playquiz(final int arrayIndex) {
setContentView(R.layout.quiz);
next = (Button) findViewById(R.id.nextBtn);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) {
int totalpoints = correctAnswerCount*10;
Intent scoreintent = new Intent(TriviaQuiz.this,ScoreBoard.class);
startActivity(scoreintent);
}
else
{
playquiz(arrayIndex+1);
}
}
What I am trying to do is, inside the method I am loading another layout and assigning an onclick for the button in that layout.
Now my problem is, the arrayIndex which I get initially, I have to update this on click of the next button and based on this I have some other conditions to check.
But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?
And even then it is not behaving in the exact way as it supposed to be.
The if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) inside onClick is not happening
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您在另一个类中使用它 - OnClickListener() 并且 arrayIndex 可能是一个局部变量。有两种方法可以解决这个问题。
您的类应该实现 OnClickListener 并重写 OnClick 方法,您必须在其中包含您的代码。
我不确定您是否在这里提供了足够的代码,但仅通过查看它,我看不到 arrayIndex 正在增加,因此它永远不会到达 TourDescription.currentTour.getTriviaArray().size()。您需要增加 arrayIndex
它在 else 子句中。
This is because you are using it inside another class - OnClickListener() and arrayIndex is probably a local variable. There are two ways to getting around this.
your class should implement OnClickListener and override OnClick method within which you must include your codes.
I am not sure if you have provided enough code here but by just looking at it i cannot see arrayIndex being incremented and hence it will never get to TourDescription.currentTour.getTriviaArray().size(). You need to increment arrayIndex
it in the else clause.