Skip to content

Instantly share code, notes, and snippets.

@jonfhancock
Last active February 25, 2021 07:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonfhancock/9534f5b05d28d1a4234fa4329243f188 to your computer and use it in GitHub Desktop.
Save jonfhancock/9534f5b05d28d1a4234fa4329243f188 to your computer and use it in GitHub Desktop.
public class DumbViewHolder extends RecyclerView.ViewHolder {
TextView textTitle;
TextView textLocation;
TextView textDate;
ImageView mapIcon;
public DumbViewHolder(View itemView) {
super(itemView);
textTitle = (TextView) itemView.findViewById(R.id.text_title);
textLocation = (TextView) itemView.findViewById(R.id.text_location);
textDate = (TextView) itemView.findViewById(R.id.text_date);
mapIcon = (ImageView) itemView.findViewById(R.id.map_icon);
}
}
public class ExcellentAdventure {
@Retention(SOURCE)
@StringDef({ERA_BC, ERA_AD})
public @interface Era {
}
public static final String ERA_BC = "BC";
public static final String ERA_AD = "AD";
private int year;
@Era private String era;
private String title;
private String lat;
private String lon;
private String locationName;
private String wikipediaTitle;
public ExcellentAdventure(int year, String era, String title,
String lat, String lon,
String locationName,
String wikipediaTitle) {
this.year = year;
this.era = era;
this.title = title;
this.lat = lat;
this.lon = lon;
this.locationName = locationName;
this.wikipediaTitle = wikipediaTitle;
}
public int getYear() { return year; }
@Era public String getEra() { return era; }
public String getTitle() { return title; }
public String getLat() { return lat; }
public String getLon() { return lon; }
public String getLocationName() { return locationName; }
public String getWikipediaTitle() { return wikipediaTitle; }
}
public class OverworkedAdapter extends RecyclerView.Adapter {
private List<ExcellentAdventure> items;
private LayoutInflater inflater;
public OverworkedAdapter(LayoutInflater inflater) {
this.inflater = inflater;
items = new ArrayList<>();
}
public void updateItems(final List<ExcellentAdventure> newItems) {
final List<ExcellentAdventure> oldItems = new ArrayList<>(this.items);
this.items.clear();
if (newItems != null) {
this.items.addAll(newItems);
}
DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return oldItems.size();
}
@Override
public int getNewListSize() {
return items.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldItems.get(oldItemPosition).equals(newItems.get(newItemPosition));
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldItems.get(oldItemPosition).equals(newItems.get(newItemPosition));
}
}).dispatchUpdatesTo(this);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.item_excellent_adventure, parent, false);
return new DumbViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DumbViewHolder vh = (DumbViewHolder) holder;
final ExcellentAdventure item = items.get(position);
vh.textTitle.setText(item.getTitle());
vh.textLocation.setText(item.getLocationName());
vh.textDate.setText(getFormattedDate(item));
vh.textTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://en.wikipedia.com/wiki/" + item.getWikipediaTitle();
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(i);
}
});
vh.mapIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = String.format("geo:%s,%s?q=%s",item.getLat(),item.getLon(),item.getLocationName());
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
view.getContext().startActivity(mapIntent);
}
});
}
private String getFormattedDate(ExcellentAdventure item) {
String date = item.getYear() + " " + item.getEra();
return date;
}
@Override
public int getItemCount() {
return items.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment