Android Open Source - AndroidPlaces Place Result Adapter






From Project

Back to project page AndroidPlaces.

License

The source code is released under:

Apache License

If you think the Android project AndroidPlaces 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

/*
 * Copyright (C) 2014 Brian Lee/*  w w w . j a va  2  s. co  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tigerpenguin.demo.places;

import android.content.Context;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.tigerpenguin.places.model.Place;
import com.tigerpenguin.places.model.PlaceLocation;
import com.tigerpenguin.places.model.PriceLevel;
import com.tigerpenguin.widget.simpleratingbar.SimpleRatingBar;

import java.math.BigDecimal;
import java.util.ArrayList;

public class PlaceResultAdapter extends ArrayAdapter<Place> {

    private PlaceLocation origin;

    public PlaceResultAdapter(Context context) {
        super(context, R.layout.places_result_item, new ArrayList<Place>());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.places_result_item, parent, false);
            convertView.setTag(new PlaceViewHolder(convertView));
        }

        PlaceViewHolder placeViewHolder = (PlaceViewHolder) convertView.getTag();
        Place place = getItem(position);

        if (place.hasPhotos()) {
            placeViewHolder.placePhoto.setVisibility(View.VISIBLE);
            place.getPhotos().get(0).loadImage(getContext(), placeViewHolder.placePhoto);
        } else {
            placeViewHolder.placePhoto.setVisibility(View.GONE);
        }

        placeViewHolder.placeName.setText(place.getName());

        if (place.hasPriceLevel()) {
            placeViewHolder.placePriceLevel.setVisibility(View.VISIBLE);
            placeViewHolder.placePriceLevel.setRating(place.getPriceLevel().ordinal());
        } else {
            placeViewHolder.placePriceLevel.setVisibility(View.INVISIBLE);
        }

        if (place.hasRating()) {
            placeViewHolder.placeRating.setVisibility(View.VISIBLE);
            placeViewHolder.placeRating.setRating(place.getRating());
        } else {
            placeViewHolder.placeRating.setVisibility(View.GONE);
        }

        PlaceLocation location = place.getLocation();
        if (origin != null && location != null) {
            float distance[] = new float[1];
            Location.distanceBetween(origin.getLatitude(), origin.getLongitude(),
                    location.getLatitude(), location.getLongitude(), distance);
            BigDecimal distanceInMiles = new BigDecimal(distance[0] * 1.6f / 1000f)
                    .setScale(1, BigDecimal.ROUND_HALF_UP);
            placeViewHolder.placeDistance.setVisibility(View.VISIBLE);
            placeViewHolder.placeDistanceUnit.setVisibility(View.VISIBLE);
            placeViewHolder.placeDistance.setText(distanceInMiles.toString());
        } else {
            placeViewHolder.placeDistance.setVisibility(View.INVISIBLE);
            placeViewHolder.placeDistanceUnit.setVisibility(View.INVISIBLE);
        }

        placeViewHolder.placeAddress.setText(place.getVicinity());

        return convertView;
    }

    public void setOrigin(double latitude, double longitude) {
        origin = new PlaceLocation(latitude, longitude);
    }

    private String getPriceLevelString(PriceLevel priceLevel) {
        switch (priceLevel) {
            case FREE:
                return "FREE";

            case INEXPENSIVE:
                return "$";

            case MODERATE:
                return "$$";

            case EXPENSIVE:
                return "$$$";

            case VERY_EXPENSIVE:
                return "$$$$";

            default:
                return "";
        }
    }

    private static class PlaceViewHolder {
        final ImageView placePhoto;
        final View placeDistanceUnit;
        final SimpleRatingBar placeRating, placePriceLevel;
        final TextView placeName, placeDistance, placeAddress;

        PlaceViewHolder(View view) {
            placePhoto = (ImageView) view.findViewById(R.id.placePhoto);
            placeName = (TextView) view.findViewById(R.id.placeName);
            placePriceLevel = (SimpleRatingBar) view.findViewById(R.id.placePriceLevel);
            placeRating = (SimpleRatingBar) view.findViewById(R.id.placeRating);
            placeDistance = (TextView) view.findViewById(R.id.placeDistance);
            placeDistanceUnit = view.findViewById(R.id.placeDistanceUnit);
            placeAddress = (TextView) view.findViewById(R.id.placeAddress);
        }
    }
}




Java Source Code List

com.tigerpenguin.demo.places.PlaceDetailActivity.java
com.tigerpenguin.demo.places.PlaceResultAdapter.java
com.tigerpenguin.demo.places.PlaceResultsFragment.java
com.tigerpenguin.demo.places.PlaceReviewAdapter.java
com.tigerpenguin.demo.places.PlaceReviewsFragment.java
com.tigerpenguin.demo.places.PlaceThumbnailAdapter.java
com.tigerpenguin.demo.places.PlaceThumbnailsFragment.java
com.tigerpenguin.demo.places.PlacesDemo.java
com.tigerpenguin.places.PlacesTest.java
com.tigerpenguin.places.deserializer.HtmlStringDeserializer.java
com.tigerpenguin.places.deserializer.PlaceTypeDeserializer.java
com.tigerpenguin.places.model.AddressComponent.java
com.tigerpenguin.places.model.AspectRating.java
com.tigerpenguin.places.model.AspectType.java
com.tigerpenguin.places.model.DayOfWeek.java
com.tigerpenguin.places.model.DayTime.java
com.tigerpenguin.places.model.Geometry.java
com.tigerpenguin.places.model.JsonModel.java
com.tigerpenguin.places.model.Language.java
com.tigerpenguin.places.model.OpeningHours.java
com.tigerpenguin.places.model.Period.java
com.tigerpenguin.places.model.Photo.java
com.tigerpenguin.places.model.PlaceDetail.java
com.tigerpenguin.places.model.PlaceLocation.java
com.tigerpenguin.places.model.PlaceType.java
com.tigerpenguin.places.model.Place.java
com.tigerpenguin.places.model.PriceLevel.java
com.tigerpenguin.places.model.RankBy.java
com.tigerpenguin.places.model.Review.java
com.tigerpenguin.places.model.StatusCode.java
com.tigerpenguin.places.request.InvalidRequestException.java
com.tigerpenguin.places.request.NearbySearchRequestTest.java
com.tigerpenguin.places.request.NearbySearchRequest.java
com.tigerpenguin.places.request.PlaceDetailRequestTest.java
com.tigerpenguin.places.request.PlaceDetailRequest.java
com.tigerpenguin.places.request.PlaceSearchRequestTest.java
com.tigerpenguin.places.request.PlaceSearchRequest.java
com.tigerpenguin.places.request.PlacesRequestTest.java
com.tigerpenguin.places.request.PlacesRequest.java
com.tigerpenguin.places.request.RequestTest.java
com.tigerpenguin.places.response.NearbySearchResponseFactory.java
com.tigerpenguin.places.response.NearbySearchResponse.java
com.tigerpenguin.places.response.PlaceDetailResponseFactory.java
com.tigerpenguin.places.response.PlaceDetailResponse.java
com.tigerpenguin.places.response.PlaceSearchResponse.java
com.tigerpenguin.places.response.PlacesResponseFactory.java
com.tigerpenguin.places.response.PlacesResponse.java
com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlaceDetailResponseHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlacesResponseHandler.java
com.tigerpenguin.widget.simpleratingbar.MockAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarTest.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBar.java