Skip to content

Instantly share code, notes, and snippets.

@hackertron
Created June 15, 2018 09:14
Show Gist options
  • Save hackertron/523f27a67db821a96d57d047c752a335 to your computer and use it in GitHub Desktop.
Save hackertron/523f27a67db821a96d57d047c752a335 to your computer and use it in GitHub Desktop.
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.RestaurantHolder> {
private ArrayList<Restaurant> mData;
private Activity mACtivity;
public RestaurantAdapter(ArrayList<Restaurant> data, Activity activity) {
this.mData = data;
this.mACtivity = activity;
}
@Override
public RestaurantHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_restaurant, parent, false);
return new RestaurantHolder(view);
}
@Override
public void onBindViewHolder(RestaurantHolder holder, int position) {
Restaurant restaurant = mData.get(position);
holder.setName(restaurant.getName());
holder.setAddress(restaurant.getAddress());
holder.setCost("Average cost for 2: " + restaurant.getCurrency() + restaurant.getCost());
holder.setRating(restaurant.getRating());
Glide.with(mACtivity)
.load(restaurant.getRating())
.into(holder.restaurantImageView);
}
@Override
public int getItemCount() {
if (mData == null)
return 0;
return mData.size();
}
public class RestaurantHolder extends RecyclerView.ViewHolder {
ImageView restaurantImageView;
TextView restaurantNameTextView;
TextView restaurantAddressTextView;
TextView restaurantRatingTextView;
TextView costTextView;
TextView distanceTextView;
public RestaurantHolder(View itemView) {
super(itemView);
restaurantImageView = (ImageView) itemView.findViewById(R.id.imageview_restaurant);
restaurantNameTextView = (TextView) itemView.findViewById(R.id.textview_restaurant_name);
restaurantAddressTextView = (TextView) itemView.findViewById(R.id.restaurant_address_textview);
restaurantRatingTextView = (TextView) itemView.findViewById(R.id.rating);
costTextView = (TextView) itemView.findViewById(R.id.cost_for_two_textview);
distanceTextView = (TextView) itemView.findViewById(R.id.restaurant_distance_textview);
}
public void setName(String name) {
restaurantNameTextView.setText(name);
}
public void setAddress(String address) {
restaurantAddressTextView.setText(address);
}
public void setRating(String rating) {
restaurantRatingTextView.setText(rating);
}
public void setCost(String cost) {
costTextView.setText(cost);
}
public void setDistance(String distance) {
distanceTextView.setText(distance);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment