Android Open Source - SurgeTracker Aggregate List Adapter

From Project

Back to project page SurgeTracker.


The source code is released under:

MIT License

If you think the Android project SurgeTracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bklimt.surgetracker.view;
// ww  w . ja v  a  2  s .c o  m
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.bklimt.surgetracker.R;
import com.bklimt.surgetracker.TimerThread;
import com.bklimt.surgetracker.model.Aggregate;
import com.bklimt.surgetracker.model.RootViewModel;

import java.lang.ref.WeakReference;

public class AggregateListAdapter extends ArrayAdapter<Aggregate> {
    public AggregateListAdapter(Context context) {
        super(context, 0);
        TimerThread.atLeastEveryMinute(new AggregateListAdapterListener(this));

     * Every 500ms, this listener gets called, and if there's a surge in progress, the list will
     * be updated so that we can see the second timer tick up. This is a static class so that we
     * can have a weak reference to "this". Otherwise, the timer thread would keep the adapter
     * around forever.
    private static class AggregateListAdapterListener implements Runnable {
        WeakReference<AggregateListAdapter> weakAdapter;

        AggregateListAdapterListener(AggregateListAdapter adapter) {
            weakAdapter = new WeakReference<AggregateListAdapter>(adapter);

        public void run() {
            AggregateListAdapter adapter = weakAdapter.get();
            if (adapter == null) {

            // TODO(klimt): This doesn't need to happen this often.
            //if (RootViewModel.get().getCurrentSurge() != null) {

    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = View.inflate(getContext(), R.layout.list_item_aggregate, null);

        final Aggregate aggregate = getItem(position);

        final TextView surgeCountView = (TextView) view.findViewById(;
        final TextView averageDurationView = (TextView) view.findViewById(;
        final TextView averageFrequencyView = (TextView) view.findViewById(;
        final TextView sinceView = (TextView) view.findViewById(;

        surgeCountView.setText(String.format("%d", aggregate.getCount()));

         * No need to bind these UI elements, because the Collection is already bound to the list
         * adapter, and that will fire change events whenever a surge changes. We wouldn't want to
         * bind these UI elements anyway, because the view can get reused.

        return view;

Java Source Code List