Android Open Source - SurgeTracker Aggregate List Adapter






From Project

Back to project page SurgeTracker.

License

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);
        }

        @Override
        public void run() {
            AggregateListAdapter adapter = weakAdapter.get();
            if (adapter == null) {
                TimerThread.removeListener(this);
                return;
            }

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

    @Override
    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(R.id.surge_count);
        final TextView averageDurationView = (TextView) view.findViewById(R.id.average_duration);
        final TextView averageFrequencyView = (TextView) view.findViewById(R.id.average_frequency);
        final TextView sinceView = (TextView) view.findViewById(R.id.since);

        surgeCountView.setText(String.format("%d", aggregate.getCount()));
        averageDurationView.setText(aggregate.getAverageDurationString());
        averageFrequencyView.setText(aggregate.getAverageTimeBetweenString());
        sinceView.setText(aggregate.getSinceTime(this.getContext()));

        /*
         * 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

com.bklimt.surgetracker.ApplicationTest.java
com.bklimt.surgetracker.SurgeTrackerApplication.java
com.bklimt.surgetracker.TimerThread.java
com.bklimt.surgetracker.backbone.CollectionListener.java
com.bklimt.surgetracker.backbone.Collection.java
com.bklimt.surgetracker.backbone.ModelListener.java
com.bklimt.surgetracker.backbone.Model.java
com.bklimt.surgetracker.backbone.Visitor.java
com.bklimt.surgetracker.model.AggregateCollection.java
com.bklimt.surgetracker.model.Aggregate.java
com.bklimt.surgetracker.model.RootViewModel.java
com.bklimt.surgetracker.model.SurgeCollection.java
com.bklimt.surgetracker.model.SurgeParseObject.java
com.bklimt.surgetracker.model.Surge.java
com.bklimt.surgetracker.view.AggregateListAdapter.java
com.bklimt.surgetracker.view.AggregateListFragment.java
com.bklimt.surgetracker.view.MainActivity.java
com.bklimt.surgetracker.view.SurgeDeletionDialogFragment.java
com.bklimt.surgetracker.view.SurgeDurationDialogFragment.java
com.bklimt.surgetracker.view.SurgeGraphFragment.java
com.bklimt.surgetracker.view.SurgeListAdapter.java
com.bklimt.surgetracker.view.SurgeListFragment.java
com.bklimt.surgetracker.view.SurgeStartDialogFragment.java