【问题标题】:Android:Go to the next item on a ListAndroid:转到列表中的下一个项目
【发布时间】:2014-01-12 11:36:33
【问题描述】:

我正在使用我的第一个 Android 应用程序:我在列表中显示了计时器列表 (CountDowntTimer)。当您勾选(onItemClick) 其中任何一个时,计时器开始运行。计时器工作得很好,但是当每个计时器结束时,我想跳转到 List 的下一个元素并执行下一个计时器。

我不知道该怎么做:也许我可以从 CountDownTimer 的 onFinish 方法中调用 onItemClick() 方法?我不知道如何使用正确的参数(AdapterView a、View v、int position、long id)来做到这一点

还有其他方法吗?可能有,但我不知道。

提前感谢您的帮助!

这里有一些代码:

lstRepeticiones.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                if(!timerHasStarted){

                String opcionSeleccionada = 
                            ((Repeticion)a.getAdapter().getItem(position)).getTitulo();
                lblEtiqueta.setText("Opción seleccionada: " + opcionSeleccionada);

                countDownTimer = new MyCountDownTimer((Integer.parseInt(datos[position].getDuracion().toString()) *1000), 1000,a , v , position , id);
                countDownTimer.start();
                timerHasStarted = true;
            }
            else{
                lblEtiqueta.setText("Elija opción");
                countDownTimer.cancel();
                timerHasStarted = false;
            }

在那里你可以看到我如何根据一些数据在每个 onItemClick 上写一个 TextView (屏幕上的一条消息)。之后,我为该选择启动相应的计时器,并在屏幕上该选项所在的位置表示 CountDownTimer。如果已经有一个计时器在运行,那么一切都会停止。

现在的想法是,当计时器结束时,我想继续处理列表中的下一项。

【问题讨论】:

  • 可以加个代码sn-p吗?

标签: android listview countdowntimer onitemclick


【解决方案1】:

正如你所说,onItemClick 是非常方便的地方。但是不要调用 onItemClick,只需在你的活动类中实现它,你需要在你的活动中添加这个实现语句:ActivityName implements AdapterView.OnItemClickListener

public void onItemClick(AdapterView<?> adp, View view, int position, long id) {
    // Do whatever you want for the current view here
    // You want to start the timer here, you can also keep a global variable of this and call it currentlyRunningTimer.
    // at onFinish method, you can start another timer

   // The next view is at position + 1, listView is a variable for your ListView
   View nextItem = listView.getChildAt(position+1);

   if(nextItem != null){
      // Do whatever you want here, for the next item.
      // you can access items in the next item like this
      TextView text = (TextView) child.findViewById(R.id.text);
   }

}

【讨论】:

  • 好的,这可以工作。让我们看看我是否能够编码。我会做一些测试并回来评论。感谢您的帮助!
  • 到目前为止,使用 getChildAt() 我已经能够检查我是否可以修改列表中的不同元素。我现在的问题是,当我从 CountDownTimer.OnFinish() 方法执行此操作时,它总是在列表的最后一个元素中更新视图。我会继续测试。
  • 只创建可见元素,因此 listView 只包含可见元素,而不是适配器拥有的所有元素。在修改 listView 项目时考虑到这一点。
  • 对!那很有意思。另一件要考虑的事情。它可以解释为什么它总是更新最后一个可见元素。但我仍然不知道为什么它不更新其余部分。仍在努力。
【解决方案2】:

为每个项目编写代码以在 onFinish() 方法中启动另一个计时器。

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         //Start another CountDownTimer
     }
  }.start();

祝你好运!

【讨论】:

  • 是的,但问题是我想在列表的下一项中显示计时器的倒计时。这就是我遇到问题的地方。
【解决方案3】:

无需以编程方式调用onItemClick(),只需直接从onItemClick() 调用代码即可。反正不看你的代码,想要给出好的答案也不容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多