全部,
我有一个列表视图,里面充满了启动和停止计时器计时器的按钮。每个计时器也设置在列表视图的每一行内。在活动内部,我有一个全局开始和停止按钮。这些按钮应该停止和启动列表视图中的所有计时器。我似乎无法引用列表视图中的所有计时器来停止它们。我什至无法引用列表视图每一行内的按钮。
下面是我的代码。请帮忙!
<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);
}}