0

全部,

我有一个列表视图,里面充满了启动和停止计时器计时器的按钮。每个计时器也设置在列表视图的每一行内。在活动内部,我有一个全局开始和停止按钮。这些按钮应该停止和启动列表视图中的所有计时器。我似乎无法引用列表视图中的所有计时器来停止它们。我什至无法引用列表视图每一行内的按钮。

下面是我的代码。请帮忙!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="wrap_content" android:id="@+id/timerPositionName"
    android:layout_height="wrap_content" android:text="blocker" />

<TextView android:layout_width="wrap_content" android:id="@+id/timerTime"
    android:layout_height="wrap_content" />

<Button android:layout_width="wrap_content" android:id="@+id/timerResetTime"
    android:layout_height="wrap_content" android:text="@string/timerResetTime"></Button>

<Button android:layout_width="wrap_content" android:id="@+id/timerStartStopTime"
    android:layout_height="wrap_content"></Button>


<Chronometer android:id="@+id/chrono"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:textStyle="bold" android:textSize="14sp" android:visibility="gone" />

这是适配器:

公共类 TimersAdapter 扩展 BaseAdapter {

final static String START = "Start";
final static String STOP = "Stop";
private PenaltyBoxTimerActivity activity;
private ArrayList<TimerController> _controllers;
private static LayoutInflater inflater = null;

public TimersAdapter(PenaltyBoxTimerActivity a, ArrayList<TimerController> controllers) {
    activity = a;
    _controllers = controllers;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return _controllers.size();
}

public Object getItem(int position) {
    return _controllers.get(position);
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder {
    public Button startStopButton;
    public Button resetButton;
    public TextView timeView;
    public TextView timerName;
    public Chronometer stopWatch;

    public long length = 0;
    public long startTime = 0;
    public long currentTime = 0;
    public long timeElapsed = 0;
    public long timeRemaining = 0;
    public long prevTimeRemaining = 0;
    public boolean isCurrentlyRunning = false;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    final ViewHolder holder;

    if (convertView == null) {
        vi = inflater.inflate(R.layout.timer_item, null);
        holder = new ViewHolder();

        holder.timeView = (TextView) vi.findViewById(R.id.timerTime);
        holder.startStopButton = (Button) vi.findViewById(R.id.timerStartStopTime);
        holder.resetButton = (Button) vi.findViewById(R.id.timerResetTime);
        holder.timerName = (TextView) vi.findViewById(R.id.timerPositionName);

        holder.stopWatch = (Chronometer) vi.findViewById(R.id.chrono);

        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    holder.length = _controllers.get(position).getLength();
    holder.startTime = _controllers.get(position).getStartTime();
    holder.currentTime = _controllers.get(position).getCurrentTime();
    holder.timeElapsed = _controllers.get(position).getTimeElapsed();
    holder.timeRemaining = _controllers.get(position).getTimeRemaining();
    holder.prevTimeRemaining = _controllers.get(position).getPrevTimeRemaining();
    holder.isCurrentlyRunning = _controllers.get(position).getIsCurrentlyRunning();
    holder.timerName.setText(_controllers.get(position).getPositionName());
    holder.timeView.setText(DateTimes.formatTime(holder.length));

    holder.stopWatch.setOnChronometerTickListener(new OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer arg0) {
            AndroidLogger.logMessage("time remaining " + DateTimes.formatTime(holder.timeRemaining), Log.INFO);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    holder.currentTime = SystemClock.elapsedRealtime();
                    holder.isCurrentlyRunning = true;
                    holder.timeElapsed = holder.currentTime - holder.startTime;
                    if (holder.prevTimeRemaining == 0)
                        holder.timeRemaining = holder.length - holder.timeElapsed;
                    else
                        holder.timeRemaining = holder.prevTimeRemaining - holder.timeElapsed;
                    holder.timeView.setText(DateTimes.formatTime(holder.timeRemaining));
                }
            });
        }
    });

    holder.resetButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AndroidLogger.logMessage("reset single Timer", Log.INFO);
            holder.prevTimeRemaining = 0;
            holder.isCurrentlyRunning = false;
            AndroidLogger.logMessage("RESET clicked ", Log.INFO);
            holder.stopWatch.setBase(SystemClock.elapsedRealtime());
            holder.timeView.setText(DateTimes.formatTime(0000));
        }
    });

    holder.startStopButton.setText(START);
    holder.startStopButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.startStopButton.getText().toString().equalsIgnoreCase(START)) {
                AndroidLogger.logMessage("start single Timer", Log.INFO);
                holder.startTime = SystemClock.elapsedRealtime();
                holder.isCurrentlyRunning = true;

                holder.stopWatch.setBase(SystemClock.elapsedRealtime());
                holder.stopWatch.start();
                holder.startStopButton.setText(STOP);
                AndroidLogger.logMessage("start clicked ", Log.INFO);
                OnTimerStartClickEvent(true);
            } else {
                AndroidLogger.logMessage("stop single Timer", Log.INFO);
                holder.currentTime = SystemClock.elapsedRealtime();
                holder.timeElapsed = holder.currentTime - holder.startTime;
                if (holder.prevTimeRemaining == 0) {
                    holder.timeRemaining = holder.length - holder.timeElapsed;
                } else {
                    holder.timeRemaining = holder.prevTimeRemaining - holder.timeElapsed;
                }
                holder.prevTimeRemaining = holder.timeRemaining;
                holder.timeView.setText(DateTimes.formatTime(holder.timeRemaining));

                holder.stopWatch.stop();

                holder.startStopButton.setText(START);
                AndroidLogger.logMessage("stop clicked ", Log.INFO);
                OnTimerStopClickEvent(false);
            }
        }
    });

    activity.setOnTimerStartClickListener(new OnTimerStartStopClickListener() {
        @Override
        public void onTimerStartStopButtonClick() {
            AndroidLogger.logMessage("is currently running:" + holder.isCurrentlyRunning, Log.INFO);
            if (holder.isCurrentlyRunning) {
                AndroidLogger.logMessage("start single Timer", Log.INFO);
                holder.startTime = SystemClock.elapsedRealtime();
                holder.isCurrentlyRunning = true;

                holder.stopWatch.setBase(SystemClock.elapsedRealtime());
                holder.stopWatch.start();
                holder.startStopButton.setText(STOP);
                AndroidLogger.logMessage("start clicked ", Log.INFO);
            }
        }
    });
    activity.setOnTimerStopClickListener(new OnTimerStartStopClickListener() {
        @Override
        public void onTimerStartStopButtonClick() {
            AndroidLogger.logMessage("is currently running:" + holder.isCurrentlyRunning, Log.INFO);
            // if (_holder.isCurrentlyRunning) {
            AndroidLogger.logMessage("stop single Timer", Log.INFO);
            holder.currentTime = SystemClock.elapsedRealtime();
            holder.timeElapsed = holder.currentTime - holder.startTime;
            if (holder.prevTimeRemaining == 0) {
                holder.timeRemaining = holder.length - holder.timeElapsed;
            } else {
                holder.timeRemaining = holder.prevTimeRemaining - holder.timeElapsed;
            }
            holder.prevTimeRemaining = holder.timeRemaining;
            holder.timeView.setText(DateTimes.formatTime(holder.timeRemaining));

            holder.stopWatch.stop();
            holder.startStopButton.setText(START);
            AndroidLogger.logMessage("stop clicked ", Log.INFO);
            // }
        }
    });

    return vi;
}

private OnTimerStartStopClickListener _onTimerStopClickListener = null;

public void setOnTimerStopClickListener(OnTimerStartStopClickListener listener) {
    _onTimerStopClickListener = listener;
}

private void OnTimerStopClickEvent(boolean isTimerCurrentlyActive) {
    if (_onTimerStopClickListener != null) {
        _onTimerStopClickListener.onTimerStartStopButtonClick();
    }
}

private OnTimerStartStopClickListener _onTimerStartClickListener = null;

public void setOnTimerStartClickListener(OnTimerStartStopClickListener listener) {
    _onTimerStartClickListener = listener;
}

private void OnTimerStartClickEvent(boolean isTimerCurrentlyActive) {
    if (_onTimerStartClickListener != null) {
        _onTimerStartClickListener.onTimerStartStopButtonClick();
    }
}}

这是我的活动:

公共类 PenaltyBoxTimerActivity 扩展 Activity { 私有按钮 _startTimersButton; 私人按钮_stopTimersButton;私有 ListView _manageTimersList;TimersListAdapter _适配器;

private ArrayList<TimerController> _timerList = new ArrayList<TimerController>();

private boolean _areCountersActive = false;
long countUp;

private OnTimerStartStopClickListener _onTimerStopClickListener = null;

public void setOnTimerStopClickListener(OnTimerStartStopClickListener listener) {
    _onTimerStopClickListener = listener;
}

private void OnTimerStopClickEvent() {
    if (_onTimerStopClickListener != null) {
        _onTimerStopClickListener.onTimerStartStopButtonClick();
    }
}

private OnTimerStartStopClickListener _onTimerStartClickListener = null;

public void setOnTimerStartClickListener(OnTimerStartStopClickListener listener) {
    _onTimerStartClickListener = listener;
}

private void OnTimerStartClickEvent() {
    if (_onTimerStartClickListener != null) {
        _onTimerStartClickListener.onTimerStartStopButtonClick();
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initializeGoToSettingsButton();
    initializeStartTimerButton();
    initializeStopTimerButton();

    // add some test data
    _timerList = new ArrayList<TimerController>();
    TimerController a = new TimerController(UUID.randomUUID(), "hello", 100000);
    TimerController b = new TimerController(UUID.randomUUID(), "hello", 60000);
    TimerController c = new TimerController(UUID.randomUUID(), "hello", 10000);
    TimerController d = new TimerController(UUID.randomUUID(), "hello", 10000);
    TimerController e = new TimerController(UUID.randomUUID(), "hello", 10000);
    TimerController f = new TimerController(UUID.randomUUID(), "hello", 10000);
    _timerList.add(a);
    _timerList.add(b);
    _timerList.add(c);
    _timerList.add(d);
    _timerList.add(e);
    _timerList.add(f);

    initializeListView();
    intializeTimerButtons();
}

private void initializeGoToSettingsButton() {
    Button goToSettingsButton = (Button) findViewById(R.id.goToSettingsButton);
    goToSettingsButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(getApplicationContext(), SettingsActivity.class);
            startActivity(myIntent);

        }
    });
}

private void initializeStopTimerButton() {
    _stopTimersButton = (Button) findViewById(R.id.stopTimersButton);
    _stopTimersButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            enableStartTimerButton();
            // OnTimerStopClickEvent();
            AndroidLogger.logMessage("stop all timers", Log.INFO);

            for (int i = 0; i < _timerList.size(); i++) {
                if (_timerList.get(i).getIsCurrentlyRunning()) {
                    // _timerList.get(i).getStopWatch().stop();

                    _timerList.get(i).setCurrentTime(SystemClock.elapsedRealtime());
                    _timerList.get(i).setTimeElapsed(
                            _timerList.get(i).getCurrentTime() - _timerList.get(i).getStartTime());

                    if (_timerList.get(i).getPrevTimeRemaining() == 0) {
                        _timerList.get(i).setTimeRemaining(
                                _timerList.get(i).getLength() - _timerList.get(i).getTimeElapsed());
                    } else {
                        _timerList.get(i).setTimeRemaining(
                                _timerList.get(i).getPrevTimeRemaining() - _timerList.get(i).getTimeElapsed());
                    }
                    _timerList.get(i).setPrevTimeRemaining(_timerList.get(i).getTimeRemaining());
                    // holder.timeView.setText(DateTimes.formatTime(holder.timeRemaining));
                }
            }

            _adapter.notifyDataSetChanged();
        }
    });
}

private void initializeStartTimerButton() {
    _startTimersButton = (Button) findViewById(R.id.startTimersButton);
    _startTimersButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AndroidLogger.logMessage("start all Timers", Log.INFO);
            enableStopTimerButton();

        }
    });
}

private void initializeListView() {
    _manageTimersList = (ListView) findViewById(R.id.manageTimersList);
    // TimersAdapter adapter = new TimersAdapter(this, R.layout.timer_item,
    // _timerList, this);
    _adapter = new TimersListAdapter(this, _timerList);
    _adapter.setOnTimerStartClickListener(new OnTimerStartStopClickListener() {
        @Override
        public void onTimerStartStopButtonClick() {
            if (!_areCountersActive) {
                enableStopTimerButton();
            }
        }
    });
    _adapter.setOnTimerStopClickListener(new OnTimerStartStopClickListener() {
        @Override
        public void onTimerStartStopButtonClick() {
            // stopTimer();
        }
    });

    _manageTimersList.setAdapter(_adapter);
    _manageTimersList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (!_areCountersActive) {
                enableStopTimerButton();
            }
        }
    });
}

private void enableStartTimerButton() {
    AndroidLogger.logMessage("start All Timers", Log.INFO);
    _startTimersButton.setEnabled(true);
    _stopTimersButton.setEnabled(false);

}

private void enableStopTimerButton() {
    AndroidLogger.logMessage("stop all Timers", Log.INFO);
    _startTimersButton.setEnabled(false);
    _stopTimersButton.setEnabled(true);

}

private void intializeTimerButtons() {
    AndroidLogger.logMessage("create timer buttons", Log.INFO);
    _startTimersButton.setEnabled(false);
    _stopTimersButton.setEnabled(false);

}}
4

2 回答 2

1

您可以尝试以下三个选项来尝试获取对您的活动的引用:

  1. 在 ListView (_manageTimersList) 上使用方法 getChildAt()。这应该让孩子处于特定位置。您可以使用 findViewById 从此处访问您的计时器,然后停止它
  2. 您可以通过适配器将 stopAll() 和 startAll() 公开给您的活动。在此方法的实现中,您将添加由 getView() 添加的任何计时器,然后在按钮调用 stopAll() 时停止它或使用 startAll() 将它们全部启动。注意:这不是我的首选方法
  3. 您传递给适配器的每个 TimerController 都可以引用您在 getView() 期间设置的 Chronometer。本质上 TimerController 成为它的包装器(我认为这可能是您的意图,但我不确定)。你基本上会做类似的事情

    TimerController timerController = _controllers.get(position);
    timerController.setChronometer(holder.stopWatch);

一旦你设置了这个引用,你就可以做你在代码中实现的事情。只需确保为列表中的每个 TimerController 检查 null 即可。

于 2011-08-20T21:17:34.790 回答
0

我认为按钮引用必须封装在_adapter. 您应该将按钮视图存储在类中的数组中(可以在getView()方法中初始化或重新初始化),TimersListAdapter并创建特殊的公共方法来切换所有按钮。您将能够从您的活动中调用的方法。

希望它会帮助你。

于 2011-08-20T20:51:44.577 回答