Android Open Source - sthlmtraveling Departure Adapter






From Project

Back to project page sthlmtraveling.

License

The source code is released under:

Apache License

If you think the Android project sthlmtraveling 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.markupartist.sthlmtraveling;
//from ww w .j  a v a 2 s  .  c om
import android.content.Context;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.markupartist.sthlmtraveling.provider.TransportMode;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.BusDeparture;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.Departures;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.DisplayRow;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.GroupOfLine;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.MetroDeparture;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.TrainDeparture;
import com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.TramDeparture;

import java.util.List;

public class DepartureAdapter extends SectionedAdapter {

    private Context mContext;

    public DepartureAdapter(Context context) {
        mContext = context;
    }

    @Override
    protected View getHeaderView(Section section, int index, View convertView, ViewGroup parent) {

        TextView result;
        try {
            result = (TextView) convertView;
        } catch (ClassCastException e) {
            return convertView;
        }

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            result = (TextView) inflater.inflate(R.layout.header, null);
        }

        result.setText(section.caption);
        return result;
    }

    public void fillDepartures(Departures departures, int transportType) {
        //TextView emptyResultView = (TextView) findViewById(R.id.departures_empty_result);
        //emptyResultView.setVisibility(View.GONE);

        this.clear();
        
        String directionString = mContext.getString(R.string.direction);

        switch (transportType) {
        case TransportMode.METRO_INDEX:
            if (departures.metros.isEmpty()) {
                //emptyResultView.setVisibility(View.VISIBLE);
            } else {
                for (MetroDeparture metroDeparture: departures.metros) {
                    for (GroupOfLine gol : metroDeparture.groupOfLines) {
                        this.addSection(0, gol.name + ", " + directionString +" 1", createAdapter(gol.direction1));
                        this.addSection(0, gol.name + ", " + directionString +" 2", createAdapter(gol.direction2));
                    }
                }
            }
            break;
        case TransportMode.BUS_INDEX:
            if (departures.buses.isEmpty()) {
                //emptyResultView.setVisibility(View.VISIBLE);
            } else {
                for (BusDeparture busDeparture : departures.buses) {
                    this.addSection(0, busDeparture.stopAreaName, createAdapter(busDeparture.departures));
                }
            }
            break;
        case TransportMode.TRAIN_INDEX:
            if (departures.trains.isEmpty()) {
                //emptyResultView.setVisibility(View.VISIBLE);
            } else {
                for (TrainDeparture trainDeparture : departures.trains) {
                    this.addSection(0, trainDeparture.stopAreaName + ", " + directionString +" 1", createAdapter(trainDeparture.direction1));
                    this.addSection(0, trainDeparture.stopAreaName + ", " + directionString +" 2", createAdapter(trainDeparture.direction2));
                }
            }
            break;
        case TransportMode.LOKALBANA_INDEX:
            if (departures.trams.isEmpty()) {
                //emptyResultView.setVisibility(View.VISIBLE);
            } else {
                for (TramDeparture tramDeparture : departures.trams) {
                    this.addSection(0, tramDeparture.stopAreaName + ", " + directionString +" 1", createAdapter(tramDeparture.direction1));
                    this.addSection(0, tramDeparture.stopAreaName + ", " + directionString +" 2", createAdapter(tramDeparture.direction2));
                }
            }
            break;
        }

        this.notifyDataSetChanged();
    }

    public static class DisplayRowAdapter extends ArrayAdapter<DisplayRow> {

        private final LayoutInflater mInflater;

        public DisplayRowAdapter(Context context, List<DisplayRow> displayRows) {
            super(context, R.layout.departures_row, displayRows);

            mInflater = (LayoutInflater) context.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DisplayRow displayRow = getItem(position);

            convertView = mInflater.inflate(R.layout.departures_row, null);

            TextView lineView = (TextView) convertView.findViewById(R.id.departure_line);
            TextView destinationView = (TextView) convertView.findViewById(R.id.departure_destination);
            TextView timeToDisplayView = (TextView) convertView.findViewById(R.id.departure_timeToDisplay);

            lineView.setText(displayRow.lineNumber);

            String destination = displayRow.destination;
            if (!TextUtils.isEmpty(displayRow.message) &&
                    TextUtils.isEmpty(destination)) {
                destination = displayRow.message;
            }
            destinationView.setText(destination);

            String displayTime = displayRow.displayTime;
            if (TextUtils.isEmpty(displayTime)
                    && TextUtils.isEmpty(displayRow.message)
                    || TextUtils.equals(displayTime, "0 min")) {
                displayTime = getContext().getString(R.string.now);
            }
            timeToDisplayView.setText(displayTime);

            return convertView;
        }
    }

    private DisplayRowAdapter createAdapter(final List<DisplayRow> displayRows) {
        return new DisplayRowAdapter(mContext, displayRows);
    }

    private String humanTimeUntil(Time start, Time end) {
        long distanceInMillis = Math.round(end.toMillis(true) - start.toMillis(true));
        long distanceInSeconds = Math.round(distanceInMillis / 1000);
        long distanceInMinutes = Math.round(distanceInSeconds / 60);

        if (distanceInMinutes <= 0.0) {
            return mContext.getString(R.string.now);
        }
        return String.format("%s min", distanceInMinutes);
    }
}




Java Source Code List

com.markupartist.sthlmtraveling.AboutActivity.java
com.markupartist.sthlmtraveling.AllTests.java
com.markupartist.sthlmtraveling.AppConfig.java
com.markupartist.sthlmtraveling.AutoCompleteStopAdapter.java
com.markupartist.sthlmtraveling.BaseActivity.java
com.markupartist.sthlmtraveling.BaseFragmentActivity.java
com.markupartist.sthlmtraveling.BaseFragment.java
com.markupartist.sthlmtraveling.BaseListActivity.java
com.markupartist.sthlmtraveling.BaseListFragmentActivity.java
com.markupartist.sthlmtraveling.BaseListFragment.java
com.markupartist.sthlmtraveling.BasePreferenceActivity.java
com.markupartist.sthlmtraveling.ChangeRouteTimeActivity.java
com.markupartist.sthlmtraveling.DepartureAdapter.java
com.markupartist.sthlmtraveling.DeparturesActivity.java
com.markupartist.sthlmtraveling.DeviationDetailActivity.java
com.markupartist.sthlmtraveling.DeviationsActivity.java
com.markupartist.sthlmtraveling.DialogHelper.java
com.markupartist.sthlmtraveling.FavoritesFragment.java
com.markupartist.sthlmtraveling.MultipleListAdapter.java
com.markupartist.sthlmtraveling.MyApplication.java
com.markupartist.sthlmtraveling.MyLocationManager.java
com.markupartist.sthlmtraveling.NearbyActivity.java
com.markupartist.sthlmtraveling.PlannerFragmentActivity.java
com.markupartist.sthlmtraveling.PlannerFragment.java
com.markupartist.sthlmtraveling.PointOnMapActivity.java
com.markupartist.sthlmtraveling.RouteDetailActivity.java
com.markupartist.sthlmtraveling.RouteParserTest.java
com.markupartist.sthlmtraveling.RoutesActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragmentActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragment.java
com.markupartist.sthlmtraveling.SectionedAdapter.java
com.markupartist.sthlmtraveling.SettingsActivity.java
com.markupartist.sthlmtraveling.StartActivity.java
com.markupartist.sthlmtraveling.TrafficStatusFragment.java
com.markupartist.sthlmtraveling.ViewOnMapActivity.java
com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter.java
com.markupartist.sthlmtraveling.provider.HistoryDbAdapter.java
com.markupartist.sthlmtraveling.provider.JourneysProvider.java
com.markupartist.sthlmtraveling.provider.PlacesProvider.java
com.markupartist.sthlmtraveling.provider.TransportMode.java
com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationNotificationDbAdapter.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationStore.java
com.markupartist.sthlmtraveling.provider.deviation.Deviation.java
com.markupartist.sthlmtraveling.provider.planner.JourneyQuery.java
com.markupartist.sthlmtraveling.provider.planner.Planner.java
com.markupartist.sthlmtraveling.provider.site.Site.java
com.markupartist.sthlmtraveling.provider.site.SitesStore.java
com.markupartist.sthlmtraveling.receivers.OnAlarmReceiver.java
com.markupartist.sthlmtraveling.receivers.OnBootReceiver.java
com.markupartist.sthlmtraveling.service.DataMigrationService.java
com.markupartist.sthlmtraveling.service.DeviationService.java
com.markupartist.sthlmtraveling.service.WakefulIntentService.java
com.markupartist.sthlmtraveling.ui.view.DelayAutoCompleteTextView.java
com.markupartist.sthlmtraveling.ui.view.LineSegment.java
com.markupartist.sthlmtraveling.ui.view.SmsTicketDialog.java
com.markupartist.sthlmtraveling.ui.view.TripView.java
com.markupartist.sthlmtraveling.utils.Analytics.java
com.markupartist.sthlmtraveling.utils.BarcodeScannerIntegrator.java
com.markupartist.sthlmtraveling.utils.DateTimeUtil.java
com.markupartist.sthlmtraveling.utils.DisplayMetricsHelper.java
com.markupartist.sthlmtraveling.utils.ErrorReporter.java
com.markupartist.sthlmtraveling.utils.HttpHelper.java
com.markupartist.sthlmtraveling.utils.IntentUtil.java
com.markupartist.sthlmtraveling.utils.LocationUtils.java
com.markupartist.sthlmtraveling.utils.StreamUtils.java
com.markupartist.sthlmtraveling.utils.StringUtils.java
com.markupartist.sthlmtraveling.utils.ViewHelper.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java