Android:如何将适配器中按钮的引用传递给活动
所有,
我有一个列表视图,其中充满了启动和停止计时器计时器的按钮。每个计时器也设置在列表视图的每一行内。在活动内部,我有一个全局开始和停止按钮。这些按钮应该停止和启动列表视图中的所有计时器。我似乎无法获取列表视图中所有计时器的引用来停止它们。我什至无法获得对列表视图每行内按钮的引用。
下面是我的代码。请帮忙!
<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" />
这是适配器:
public class TimersAdapter extends 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();
}
}}
这是我的 Activity:
public class PenaltyBoxTimerActivity extends 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);
}}
All,
I have a listview filled with Buttons that start and stop a chronometer timer. Each timer is also set inside each row of the listview. Inside the activity, I have a global start and stop button. These buttons are supposed to stop and start all the timers in the listview. I can't seem to get a reference to all the timers in the listview in order to stop them. I can't even get a reference to the buttons inside each row of the listview.
Below is my code. Please help!
<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" />
Here is the Adapter:
public class TimersAdapter extends 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();
}
}}
Here is my Activity:
public class PenaltyBoxTimerActivity extends Activity {
private Button _startTimersButton;
private Button _stopTimersButton;
private ListView _manageTimersList;
TimersListAdapter _adapter;
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);
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下三个选项来尝试获取对您的活动的引用:
一旦设置了此引用,您就可以执行在代码中实现的操作。只需确保检查列表中每个 TimerController 是否为 null。
You could try these three options to try getting the reference out to your activity:
Once you set this reference, you could do what you have implemented in the code. Just make sure to check for null for each TimerController in the list.
我认为按钮引用必须封装在
_adapter
中。您应该将按钮视图存储在
TimersListAdapter
类中的数组中(可以在getView()
方法中初始化或重新初始化),并创建特殊的公共方法来切换所有按钮。您将能够从您的活动中调用的方法。希望它能帮助你。
I think button references must be incapsulated in
_adapter
.You should store button views in array (which can be initialized or reinitialized within
getView()
method) inTimersListAdapter
class and create special public method to toggle all buttons. The method you will be able to invoke from your activity.Hope it will help you.