Android Open Source - NearestRestaurants Restaurant List Adapter






From Project

Back to project page NearestRestaurants.

License

The source code is released under:

Apache License

If you think the Android project NearestRestaurants 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.jiahaoliuliu.nearestrestaurants;
/*  w  ww  .  j  a  v  a 2 s  .  c  om*/
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jiahaoliuliu.nearestrestaurants.models.Restaurant;

/**
 * The base adapter for the list of the restaurants.
 */
public class RestaurantListAdapter extends BaseAdapter {
    
    private Context context;
    private List<Restaurant> restaurants;
    private LayoutInflater inflater;
    
    public RestaurantListAdapter(Context context, List<Restaurant> restaurants) {
        this.context = context;
        this.restaurants = restaurants;
        inflater = LayoutInflater.from(context);
    }

    // Reset the list of the restaurants
    public void setRestaurants(List<Restaurant> restaurants) {
        this.restaurants = restaurants;
        notifyDataSetChanged();
    }

    // Add new restaurants to the list
    public void addMoreRestaurants(List<Restaurant> newRestaurants) {
      this.restaurants.addAll(newRestaurants);
      notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return restaurants.size();
    }

    @Override
    public Object getItem(int position) {
        return restaurants.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        // Keeps reference to avoid future findViewById()
      RestaurantViewHolder restaurantViewHolder;
 
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_layout, viewGroup, false);
 
            restaurantViewHolder = new RestaurantViewHolder();
            restaurantViewHolder.nameTextView =
                (TextView) convertView.findViewById(R.id.restaurantNameTextView);
            restaurantViewHolder.vicinityTextView =
                (TextView) convertView.findViewById(R.id.restaurantVicinityTextView);

            convertView.setTag(restaurantViewHolder);
        } else {
          restaurantViewHolder = (RestaurantViewHolder) convertView.getTag();
        }
 
        Restaurant restaurant = restaurants.get(position);

        // Set the name
        restaurantViewHolder.nameTextView.setText(restaurant.getName());
        
        // Set the vicinity
        String vicinity = restaurant.getVicinity();
        if (vicinity != null && !vicinity.equalsIgnoreCase("")) {
          restaurantViewHolder.vicinityTextView.setText(vicinity);
        }
        
        return convertView;
    }
    
    static class RestaurantViewHolder {
        TextView nameTextView;
        TextView vicinityTextView;
    }
}




Java Source Code List

com.jiahaoliuliu.nearestrestaurants.NearestRestaurantsListFragment.java
com.jiahaoliuliu.nearestrestaurants.NearestRestaurantsMapFragment.java
com.jiahaoliuliu.nearestrestaurants.NearestRestaurants.java
com.jiahaoliuliu.nearestrestaurants.RestaurantListAdapter.java
com.jiahaoliuliu.nearestrestaurants.interfaces.Callback.java
com.jiahaoliuliu.nearestrestaurants.interfaces.ErrorCallback.java
com.jiahaoliuliu.nearestrestaurants.interfaces.OnPositionRequestedListener.java
com.jiahaoliuliu.nearestrestaurants.interfaces.OnProgressBarShowRequestListener.java
com.jiahaoliuliu.nearestrestaurants.interfaces.OnRefreshRequestedListener.java
com.jiahaoliuliu.nearestrestaurants.interfaces.OnUpdatePositionListener.java
com.jiahaoliuliu.nearestrestaurants.interfaces.RequestDataCallback.java
com.jiahaoliuliu.nearestrestaurants.interfaces.RequestJSONCallback.java
com.jiahaoliuliu.nearestrestaurants.interfaces.RequestRestaurantsCallback.java
com.jiahaoliuliu.nearestrestaurants.interfaces.RequestStringCallback.java
com.jiahaoliuliu.nearestrestaurants.models.Restaurant.java
com.jiahaoliuliu.nearestrestaurants.session.ErrorHandler.java
com.jiahaoliuliu.nearestrestaurants.session.HttpRequest.java
com.jiahaoliuliu.nearestrestaurants.session.Preferences.java
com.jiahaoliuliu.nearestrestaurants.session.RestaurantDBAdapter.java
com.jiahaoliuliu.nearestrestaurants.session.Service.java
com.jiahaoliuliu.nearestrestaurants.session.Session.java
com.jiahaoliuliu.nearestrestaurants.utils.Connectivity.java
com.jiahaoliuliu.nearestrestaurants.utils.PositionTracker.java