更新按钮的 arrayIndex onClick 导致问题

发布于 2024-12-04 20:23:51 字数 1023 浏览 0 评论 0原文

我正在实现一个测验,这里有一个按钮的方法,因为

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-12-11 20:23:51
But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?

这是因为您在另一个类中使用它 - OnClickListener() 并且 arrayIndex 可能是一个局部变量。有两种方法可以解决这个问题。

  1. 在全局声明区声明。
  2. 您的类应该实现 OnClickListener 并重写 OnClick 方法,您必须在其中包含您的代码。

    onClick 内的 if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) 没有发生
    

我不确定您是否在这里提供了足够的代码,但仅通过查看它,我看不到 arrayIndex 正在增加,因此它永远不会到达 TourDescription.currentTour.getTriviaArray().size()。您需要增加 arrayIndex
它在 else 子句中。

But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?

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.

  1. declare it in the global declaration area.
  2. your class should implement OnClickListener and override OnClick method within which you must include your codes.

    The if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) inside onClick is not happening
    

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文