Android:如何将适配器中按钮的引用传递给活动

发布于 2024-11-30 17:48:17 字数 15670 浏览 2 评论 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" />

这是适配器:

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

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

发布评论

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

评论(2

临风闻羌笛 2024-12-07 17:48:17

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

  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。

You could try these three options to try getting the reference out to your activity:

  1. Use the method getChildAt() on your ListView (_manageTimersList). This should get the child at a specific position. You could access your chronometer from here using findViewById then stop it
  2. You can expose the stopAll() and startAll() to the your activity via your adapter. In the implementation of this method, you would add any chronometers added by getView() and then just stop it when the button call stopAll() or start them all using startAll(). Note: this is not my preferred method
  3. Each TimerController that you pass to the adapter can have reference to the Chronometer that you set during getView(). In essence TimerController becomes the wrapper of it (which I think might be your intention, but I am not sure). You basically will be doing something like

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

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.

诗酒趁年少 2024-12-07 17:48:17

我认为按钮引用必须封装在 _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) in TimersListAdapter 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.

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